NameValueCollection vs Dictionary<string,string> [duplicate]
Solution 1:
They aren't semantically identical. The NameValueCollection
can have duplicate keys while the Dictionary
cannot.
Personally if you don't have duplicate keys, then I would stick with the Dictionary
. It's more modern, uses IEnumerable<>
which makes it easy to mingle with Linq
queries. You can even create a Dictionary
using the Linq
ToDictionary()
method.
Solution 2:
NameValueCollection
is string
typed whereas Dictionary
leverages generics to allow type variance. See Benefits of Generics.
Solution 3:
Dictionary
will be much faster. NameValueCollection
allows duplicate keys. Which could be bad in certain situations, or desired in other. Dictionary
does not allow duplicate keys.
From: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
The
Dictionary<(Of <(TKey, TValue>)>)
generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close toO(1)
, because theDictionary<(Of <(TKey, TValue>)>)
class is implemented as a hash table.