KeyValuePair列表和相同类型的字典之间有什么区别?有适当的时间使用其中一种吗?您何时使用List <KeyValuePair <T1, T2>>而不是字典<T1, T2>?
回答
当你不需要在密钥上快速查找时 - 维护Dictionary
使用的哈希表具有一定的开销。
从http://blogs.msdn.com/bclteam/archive/2004/09/03/225473.aspx:
KeyValuePair
与DictionaryEntry
[克齐斯茨托夫·克瓦林纳]我们与 实施
IEnumerable
上Dictionary<K,V>
讨论的一个问题。IEnumerable.GetEnumerator().Current
应返回什么类型?KeyValuePair<K,V>
或DictionaryEntry
?相同的ICollection.CopyTo
。什么 类型应该被复制到数组的实例?我们决定如下:
IEnumerable
和ICollection
接口 实施将使用KeyValuePair<K,V>
作为项目类型。IDictionary
特定会员 (GetEnumerator
返回IDictionaryEnumerator
)将使用DictionaryEntry
作为项目类型。的原因是,我们是在做一个改变,其中
IEnumerator<T>
将延长IEnumerator
的过程 。如果从Dictionary<K,V>
→IEnumerable<T>
→IEnumerable
我们突然改变了从枚举数返回的 项的类型,这将是非常奇怪的 。
在用于silverlight的SOAP webservices中,我们发现Dictionary不会序列化。这将是一种情况,您可以在Dictionary上使用KeyValuePair列表。
。
当你关心的项目的顺序列表也将是有益的。
不会[SortedDictionary](https://msdn.microsoft.com/en-us/library/f7fta44c.aspx)覆盖此? – 2015-02-18 00:11:15
是的,但SortedDictionary无法覆盖值的顺序,只有键。 – ConfusedMan 2015-04-15 21:04:44
除了Phillip Ngan的答案,SOAP或其他,你不能XML序列化实现IDictionary的对象。
问:为什么我不能序列化哈希表?
答:XmlSerializer无法处理实现IDictionary接口的类。这部分是由于进度的限制,部分是由于这样的事实,一个哈希表没有在XSD类型系统的配对。唯一的解决方案是实现一个不实现IDictionary接口的自定义哈希表。
字典是泛型类型包含键值对的集合。由于在内部使用散列函数,所以字典对于查找操作是快速的。这意味着,所有密钥必须在字典中唯一。
考虑这方面的例子:
List<KeyValuePair<int, string>> pairs = new List<KeyValuePair<int, string>>();
pairs.Add(new KeyValuePair<int, string>(1, "Miroslav"));
pairs.Add(new KeyValuePair<int, string>(2, "Naomi"));
pairs.Add(new KeyValuePair<int, string>(2, "Ingrid"));
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "Miroslav");
dict.Add(2, "Naomi");
dict.Add(2, "Ingrid"); // System.ArgumentException: An item with the same key has already been added.
所以,你应该考虑两个至少两件事情:
- 你要搜索的字典具体项目?
- 是否需要某些字段 非唯一(例如pair:firstname/lastname)。
我认为这里的要点是字典键必须是唯一的,其中List
@BrunoBieri List
@niksofteng你是绝对正确的。我的错。 – 2017-06-06 19:57:54
- 1. Tuple之间的转换<T1,T2>和KeyValuePair <T1, T2>
- 2. 选择字典<T1, T2> with LINQ
- 3. 元组如何创建<T1,T2>。创建<T1,T2>(T1 item1,T2 item2)?
- 4. List <Tuple<T1,T2>>作为GridView的DataSource
- 5. ASP.NET中继器 - 显示属性列表<T1> Databound List <T>
- 6. 将IList <T1>转换为IList <BaseT1>
- 7. Simply IEnumerable <int> to List <KeyValuePair <int, int>>?
- 8. 表达式<Func <T1,T2,TResult >>和Sql IN
- 9. Promise的TypeScript参数<T>,Why not Promise <T1,T2>?
- 10. .NET MVC的ViewPage <T1>和ViewUserControl <T2>行为
- 11. 序列化一个List <KeyValuePair <字符串,对象>>
- 12. 不能从`Expression <Func<T1, T2>>`转换为`Expression <Func <object,object >>`
- 13. 为什么从模板<typename T1,typename T2>到模板<typename T1,int i>不支持类专业化
- 14. 从IEnumerable重新创建一个字典<KeyValuePair <>>
- 15. 如何将List <KeyValuePair <int,int>>转换为int [] []?
- 16. 如何表达<Func键<T1, bool>>转换为表达<Func键<T2, bool>>
- 17. 转换的IOrderedEnumerable <KeyValuePair <string, int>>成字典<string, int>
- 18. 验证服务方法 - “T2验证<T1, T2>(表达式,T2)”vs“对象验证<T1>(Expression,object)”
- 19. LINQ选择新KeyValuePair <T,List<T2>>
- 20. 转换表达<Func键<T1,bool>>表达式<Func键<T2,bool>动态
- 21. 无法了解如何编写代码Func键<Func<T1, T2>,T3>
- 22. 排序列表<keyValuePair <字符串,字符串>>
- 23. FileHelperEngine返回列表<object>而不是List <T>
- 24. JAXB XJC创建列表< Objects >而不是List < String >
- 25. 字典用List <>作为TValue
- 26. LINQ的 - 从表达式创建表达式<T1><T2>
- 27. 是字典中KeyValuePair <>元素的迭代顺序<int,T>
- 28. 如何将数据表转换为List <long,List <keyvaluepair <string,string >>> in C#
- 29. 在ICollection上实现.Contains()的最简单方法<Tuple<T1,T2>>
- 30. 如何将值设置为:KeyValuePair <KeyValuePair <String,String>,int>
另外列表插入操作是更快的,在字典 – 2009-11-20 09:27:01
是不是KeyValuePair是只读? – 2016-12-27 04:55:34
其字段是只读的,但您可以随时替换列表中的整个元素。 – 2016-12-27 06:21:09