List<KeyValuePair<TKey,TValue>> 与 Dictionary<TKey,TValue> 不同

两者都可以通过 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)

考虑是否可以利用第三个参数做一些事情,有待验证

转载于:https://www.cnblogs.com/ArvinZhao/p/6004390.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值