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?
7 Answers
When you don't need fast lookups on key - maintaining the hashtable used by Dictionary
has a certain overhead.
-
12Also list insert operation is faster that the one in Dictionary Commented Nov 20, 2009 at 9:27
-
3Its fields are readonly, but you can always replace the entire element in the list. Commented Dec 27, 2016 at 6:21
In short, the list does not enforce uniqueness of the key, so if you need that semantic then that's what you should use.
-
9+1 Note that dictionary doesn't enforce uniqueness of the value either!– gdoronCommented Nov 8, 2011 at 9:36
Dictionary is generic type that 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.
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:
- Do you want to search concrete items in dictionary?
- Do you want to have some fields non-unique (for example pairs: firstname/lastname).
-
2I think the point here is that dictionary keys must be unique where List<KeyValuePair> keys must not be unique. Commented Dec 21, 2015 at 7:35
-
9@BrunoBieri List<KeyValuePair> keys may not be unique Commented Jun 6, 2017 at 14:44
-
2I rectified your 2 yrs old comment, and you noticed that. No wonder why SO is the only trusted and most popular Q&A platform. Commented Jun 6, 2017 at 20:15
The List would also be useful when you care about the order of the items.
-
2
-
2Yes, but SortedDictionary cannot cover the order of the values, only the keys. Commented Apr 15, 2015 at 21:04
Further to Phillip Ngan's answer, SOAP or otherwise, you cannot XML serialize objects that implements 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.
-
Paul Welter wrote a serializable XML Dictionary weblogs.asp.net/pwelter34/444961– JoeCommented Jun 29, 2023 at 19:19
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.
.
From http://blogs.msdn.com/bclteam/archive/2004/09/03/225473.aspx:
KeyValuePair
vs.DictionaryEntry
[Krzysztof Cwalina]We discussed a problem with implementation of
IEnumerable
onDictionary<K,V>
. What type shouldIEnumerable.GetEnumerator().Current
return?KeyValuePair<K,V>
orDictionaryEntry
? Same forICollection.CopyTo
. Instances of what type should be copied to the array?We decided the following:
IEnumerable
andICollection
interface implementations will useKeyValuePair<K,V>
as the item type.IDictionary
specific members (GetEnumerator
returningIDictionaryEnumerator
) will useDictionaryEntry
as the item type.The reason is that we are in a process of making a change where
IEnumerator<T>
would extendIEnumerator
. It would be very strange if walking the hierarchy fromDictionary<K,V>
->IEnumerable<T>
->IEnumerable
we suddenly changed the type of the item returned from enumerators.