兩者都可以通過
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)`
考慮是否可以利用第三個參數做一些事情,有待驗證
