c# – ListDictionary類有一個通用的替代方法嗎?
T>中有足夠的開銷物件來證明非通用ListDictionary的開銷?
將使用此物件的程式碼將列舉在每個頁面載入上,我猜想是為什麼ListDictionary類被用於其他替代方法之一.這也是為什麼我希望在這個資料列表中表現最好的.
不幸的是沒有類似於ListDictionary的泛型.
但是,實施這一點並不是非常困難的. ListDictionary基本上通過保留Key / Value對的連結串列,並對它們進行查詢操作.您可以建立一個ListDictionary&TK; TKalue>通過包裝LinkedList<T>有一些非常簡單的LINQ表示式.
例如
public class LinkedDictionary<TKey,TValue> { private LinkedList<KeyValuePair<TKey,TValue>> _list = new LinkedList<KeyValuePair<TKey,TValue>>(); private IEqualityComparer<TKey> _comp = EqualityComparer<TKey>.Default; public void Add(TKey key, TValue value) { _list.Add(new KeyValuePair<TKey,TValue>(key,value)); } public TValue Get(TKey key) { return _list.Where(x => _comp.Equals(x.Key,key)).First().Value; } ... }
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/1234831/is-there-a-generic-alternative-to-the-listdictionary-class