13

There are other questions such as KeyValuePair vs IDictionary, but I feel this one differs slightly.

NameValueCollection takes a string key and string value.

KeyValuePair is like a dictionary, you tell it what type the key and value is.

I don't understand why NameValueCollection exists. Initializing a KeyValuePair with string types seems sufficient. I also noticed that NameValueCollection has some more methods available to it, but again why not merge both classes into one?

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
  • 10
    A `KeyValuePair` not like a dictionary. It is simply a Tuple containing the Key and the Value. –  May 27 '11 at 01:32
  • @pst, a dictionary takes a list of key/value pairs. I'm failing to see a difference. – The Muffin Man May 27 '11 at 01:39
  • @Nick `var kvp = new KeyValuePair("Hello", "World!")` -- [KeyValuePair](http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx) –  May 27 '11 at 01:44
  • @Nick: A dictionary can only have one of every key. – soandos May 27 '11 at 01:44
  • I believe my misunderstanding was that a dictonary holds key value pairs, where as a key value pair is singular, you can't keep adding to pst's `kvp` example above for instance. – The Muffin Man May 27 '11 at 02:02
  • @Nick: @pst means that A dictionary is a collection of kvps where you can't have duplicate keys, while a kvp is just one instance of a kvp. seems fairly straightforward – bevacqua May 27 '11 at 02:03
  • A KeyValuePair stores a single key and a single value. That's it. Not multiple keys vs multiple values... just one of each. It's not a collection, but simply a container for a pair of objects. – spender May 27 '11 at 02:04
  • @Nico, @spender, I understand now, thank you for the clearer explanation. – The Muffin Man May 27 '11 at 17:03

3 Answers3

19

A KeyValuePair not like a dictionary. It is simply a Tuple containing the Key and the Value.

NameValueCollection is wrapper over what amounts to a IList<KeyValuePair<string,IList<string>>> (note that NameValueCollection predates generics) - operations like Get(string) are O(n) and items can be fetched by index and each Key maps to one or more Values (this differs from a Dictionary<string,string>).

A reason for this is explained in the NameValueCollection documentation:

This class can be used for headers, query strings and form data.

The newer "replacement" data-structure with some similar behavior for NameValueCollection is Lookup<string,string>. (However, it doesn't directly support the same operations as is immutable as spender notes.)

Happy coding.

  • 1
    The principal problem with Lookup in this scenario is that it is immutable, unlike NaveValueCollection, which can be added to. As such, it's not really a "replacement", but has similar behaviour. – spender May 27 '11 at 02:07
4

NameValueCollection existing in .NET 1.0 and 1.1, KeyValuePair is a generic type and wasn't added to .NET until 2.0. All the classes in System.Collections.Specialized all predates the addition of generics; it contains certain strongly typed (specialized if you will) for use when that's exactly what you need to users don't have to cast from object to string.

shf301
  • 31,086
  • 2
  • 52
  • 86
0

KeyValuePair is the component you use to iterate a Dictionary

var dictionary = new Dictionary<int,long>

foreach(var kvp in dictionary)
{
    // kvp is KeyValuePair<int,long>. kvp.Key is the int key and kvp.Value is the long value for the key
}

NameValueCollection is indexable.

bevacqua
  • 47,502
  • 56
  • 171
  • 285