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?
A
KeyValuePairnot like a dictionary. It is simply a Tuple containing the Key and the Value.NameValueCollectionis wrapper over what amounts to aIList<KeyValuePair<string,IList<string>>>(note thatNameValueCollectionpredates generics) – operations likeGet(string)areO(n)and items can be fetched by index and each Key maps to one or more Values (this differs from aDictionary<string,string>).A reason for this is explained in the NameValueCollection documentation:
The newer
“replacement”data-structure with some similar behavior forNameValueCollectionisLookup<string,string>. (However, it doesn’t directly support the same operations as is immutable as spender notes.)Happy coding.