List > 與 Dictionary 不同


兩者都可以通過 KeyValuePair<TKey,TValue> 進行遍歷,並且兩者可以相互轉換:

List<KeyValuePair<string,string>> list = new List<KeyValuePair<string, string>>();
list.Add(new KeyValuePair<string, string>("asdf1", "1"));
list.Add(new KeyValuePair<string, string>("asdf1", "1"));
list.Add(new KeyValuePair<string, string>("asdf1", "1"));
Dictionary<string, string> dictionary = list.ToDictionary(x => x.Key, x => x.Value);
list = dictionary.ToList();


> 但是,后者的Key是唯一的,序列中不能包含相同Key值,前者就沒有這個要求了。
> 因此,在將前者轉化為后者時,如果有相同的key值,就會報錯。
> 此外,
> `Enumarable.ToDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)`

考慮是否可以利用第三個參數做一些事情,有待驗證


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM