[英]Is there a collection with both Contains and IndexOf methods that are faster than a List?
我正在構建一個序列化系統,該系統在文件的開頭放置一個類型表,以便要序列化的每個字段都可以在實際數據之前簡單地存儲其類型的索引(而不是在每個字段中存儲完整的類型名稱)。 為此,我需要:
是否存在這樣的集合?
您所描述的已經存在,它被稱為...一本字典。 您只需要以另一種方式解決問題:索引是您的值,而不是您的鍵。
您的方法可能如下所示:
private Dictionary<Type, int> serializationTable = new Dictionary<Type, int>();
public int GetSerializationIndex(Type type)
{
int index;
if (!this.serializationTable.TryGetValue(type, out index))
{
index = this.serializationTable.Count;
this.serializationTable.Add(type, index);
}
return index;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.