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

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


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


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:

  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).

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