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. struct s {T0 t0; T1 t1; ...}和std :: tuple <T0,T1,...> t
- 6. 词典<字符串,MyClass的>为List <KeyValuePair <字符串,字符串>>其中KeyValuePair值是MyClass.ToString()
- 7. 如何转换List <KeyValuePair <string,decimal >> list = new List <KeyValuePair <string,decimal >>();到DataTable?
- 8. 如何循环两对STL集<pair<t1,t2>,对<t1,t2>>中的所有元素?
- 9. 如何使用矢量<T2>代替矢量<T1>,当T2是C++中T1的子类?
- 10. Java字典<String,List <Object>>
- 11. 如何从IEnumerable <T1>中检索IEnumerable <T2>?
- 12. ASP.NET中继器 - 显示属性列表<T1> Databound List <T>
- 13. 将IList <T1>转换为IList <BaseT1>
- 14. Simply IEnumerable <int> to List <KeyValuePair <int, int>>?
- 15. 实现抽象方法返回类型<T1<T2>>
- 16. 请求体的List <KeyValuePair <字符串,字符串>>
- 17. 表达式<Func <T1,T2,TResult >>和Sql IN
- 18. 转换授人以行动<T>(动作<T1,T2>)
- 19. 构造,从类型<T1>
- 20. .NET MVC的ViewPage <T1>和ViewUserControl <T2>行为
- 21. Promise的TypeScript参数<T>,Why not Promise <T1,T2>?
- 22. 序列化一个List <KeyValuePair <字符串,对象>>
- 23. 不能从`Expression <Func<T1, T2>>`转换为`Expression <Func <object,object >>`
- 24. 为什么从模板<typename T1,typename T2>到模板<typename T1,int i>不支持类专业化
- 25. 从IEnumerable重新创建一个字典<KeyValuePair <>>
- 26. 如何将List <KeyValuePair <int,int>>转换为int [] []?
- 27. 如何表达<Func键<T1, bool>>转换为表达<Func键<T2, bool>>
- 28. 从2个表选择时t1.id = t2.id和t1.name <> t2.name
- 29. 演员行动<T1>行动<T2>在c#中,其中T1和T2没有关系
- 30. 转换的IOrderedEnumerable <KeyValuePair <string, int>>成字典<string, int>
另外列表插入操作是更快的,在字典 – 2009-11-20 09:27:01
是不是KeyValuePair是只读? – 2016-12-27 04:55:34
其字段是只读的,但您可以随时替换列表中的整个元素。 – 2016-12-27 06:21:09