C# 什么时候使用 List<KeyValuePair<T1, T2>> 而不是 Dictionary<T1, T2> ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1769053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:39:17  来源:igfitidea点击:

When would you use a List<KeyValuePair<T1, T2>> instead of a Dictionary<T1, T2>?

c#dictionary

提问by Corpsekicker

What is the difference between a List of KeyValuePair and a Dictionary for the same types? Is there an appropriate time to use one or the other?

相同类型的 KeyValuePair 列表和字典有什么区别?是否有合适的时间使用其中之一?

采纳答案by Pavel Minaev

When you don't need fast lookups on key - maintaining the hashtable used by Dictionaryhas a certain overhead.

当您不需要对键进行快速查找时 - 维护使用的哈希表Dictionary有一定的开销。

回答by RCIX

In short, the list does not enforce uniqueness of the key, so if you need that semantic then that's what you should use.

简而言之,该列表不强制密钥的唯一性,因此如果您需要该语义,那么这就是您应该使用的。

回答by Anax

From http://blogs.msdn.com/bclteam/archive/2004/09/03/225473.aspx:

http://blogs.msdn.com/bclteam/archive/2004/09/03/225473.aspx

KeyValuePairvs. DictionaryEntry
[Krzysztof Cwalina]

We discussed a problem with implementation of IEnumerableon Dictionary<K,V>. What type should IEnumerable.GetEnumerator().Currentreturn? KeyValuePair<K,V>or DictionaryEntry? Same for ICollection.CopyTo. Instances of what type should be copied to the array?

We decided the following: IEnumerableand ICollectioninterface implementations will use KeyValuePair<K,V>as the item type. IDictionaryspecific members (GetEnumeratorreturning IDictionaryEnumerator) will use DictionaryEntryas the item type.

The reason is that we are in a process of making a change where IEnumerator<T>would extend IEnumerator. It would be very strange if walking the hierarchy from Dictionary<K,V>->IEnumerable<T>->IEnumerablewe suddenly changed the type of the item returned from enumerators.

KeyValuePair对阵DictionaryEntry
[Krzysztof Cwalina]

我们讨论了IEnumerableon 的 实现问题Dictionary<K,V>。应该IEnumerable.GetEnumerator().Current返回什么类型 ?KeyValuePair<K,V>或者 DictionaryEntry?对于 ICollection.CopyTo. 什么类型的实例应该复制到数组中?

我们决定如下:IEnumerableICollection接口实现将 KeyValuePair<K,V>用作项目类型。 IDictionary特定成员(GetEnumerator返回 IDictionaryEnumerator)将 DictionaryEntry用作项目类型。

原因是我们正在对IEnumerator<T>会扩展的 地方进行更改 IEnumerator。如果从Dictionary<K,V>-> IEnumerable<T>->遍历层次结构,IEnumerable我们突然改变了从枚举器返回的项目的类型,那将是非常奇怪的 。

回答by Phillip Ngan

In SOAP webservices for silverlight, we have found that Dictionary's do not serialize. This would be a situation where you would use a List of KeyValuePair over a Dictionary.

在 Silverlight 的 SOAP 网络服务中,我们发现 Dictionary 不会序列化。在这种情况下,您将在字典上使用 KeyValuePair 列表。

.

.

回答by nobody

The List would also be useful when you care about the order of the items.

当您关心项目的顺序时,列表也很有用。

回答by tjmoore

Further to Phillip Ngan's answer, SOAP or otherwise, you cannot XML serialize objects that implements IDictionary.

除了 Phillip Ngan 的回答,SOAP 或其他方面,您不能通过 XML 序列化实现 IDictionary 的对象。

Q: Why can't I serialize hashtables?

A: The XmlSerializer cannot process classes implementing the IDictionary interface. This was partly due to schedule constraints and partly due to the fact that a hashtable does not have a counterpart in the XSD type system. The only solution is to implement a custom hashtable that does not implement the IDictionary interface.

问:为什么我不能序列化哈希表?

答:XmlSerializer 无法处理实现 IDictionary 接口的类。这部分是由于计划限制,部分是由于散列表在 XSD 类型系统中没有对应的事实。唯一的解决方案是实现一个不实现 IDictionary 接口的自定义哈希表。

from here

从这里

回答by Miroslav Holec

Dictionary is generic typethat contains a collection of key-value pairs. Dictionary is fast for lookup operations, because is using hash function internally. That means, all the keys must be unique in dictionary.

字典是包含键值对集合的泛型类型。字典对于查找操作很快,因为在内部使用散列函数。这意味着,所有键在 dictionary 中必须是唯一的

Consider this examples:

考虑这个例子:

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.

So you should always consider two at least two things:

因此,您应该始终考虑至少两件事:

  1. Do you want to search concrete items in dictionary?
  2. Do you want to have some fields non-unique (for example pairs: firstname/lastname).
  1. 您想在字典中搜索具体项目吗?
  2. 您是否希望某些字段不唯一(例如对:名字/姓氏)。