NameValueCollection
This C# type allows many values for one key. It is found in System.Collections
Specialized, and it does not provide the best performance.
Other collections are likely to be faster than NameValueCollection
. Unless you need to use NameValueCollection
, you should consider Dictionary
or List
.
We add 4 string
key-value pairs to a NameValueCollection
. The keys can occur more than once, but map to the same array of values. You can have one key pointing to two values.
NameValueCollection
. The collection maps two keys to 4 values.AllKeys
property. We use it in a foreach
-loop to see all the keys in the collection.null
if the key is not found. If more than one value is found, it returns a string
value joined by commas.HasKeys
method, NameValueCollection
can test whether there are keys.GetKey
with an index to gets the key at that index. This method always receives an int
, not a string
.int
(like GetKey
).using System; using System.Collections.Specialized; class Program { static void Main() { // Part 1: create new NameValueCollection. var collection = new NameValueCollection(); collection.Add("cat", "orange"); collection.Add("dog", "brown"); collection.Add("dog", "white"); collection.Add("cat", "black"); // Part 2: loop over keys. foreach (string key in collection.AllKeys) { Console.WriteLine(key); } // Part 3: get values. Console.WriteLine(collection["cat"]); Console.WriteLine(collection["x"] == null); // Part 4: use HasKeys. Console.WriteLine(collection.HasKeys()); // Part 5: use GetKey. Console.WriteLine(collection.GetKey(0)); // Part 6: get the first value. string value = collection.Get(0); Console.WriteLine(value); } }cat dog orange,black True True cat orange,black
Does the NameValueCollection
have competitive performance to the Dictionary
? In some programs, we can use a NameValueCollection
instead of a Dictionary
.
NameValueCollection
in this version.Dictionary
and access a value associated with a key. We use the same key as in the other version.NameValueCollection
lookup speed on even a small collection is poor. Its performance is likely much worse in many situations.#define VERSION1 using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; class Program { public static void Main() { var collection = new NameValueCollection(); collection.Add("cat", "orange"); collection.Add("dog", "brown"); var dictionary = new Dictionary<string, string>(); dictionary.Add("cat", "orange"); dictionary.Add("dog", "brown"); const int _max = 10000000; var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { #if VERSION1 string value = collection["dog"]; #endif #if VERSION2 string value = dictionary["dog"]; #endif } s1.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } }89.20 ns NameValueCollection 10.83 ns Dictionary
The NameValueCollection
is prevalent in ASP.NET, and it is used in appSettings
, QueryString
, and Headers collections. In many projects it is used with appSettings
.
NameValueCollection
is located in the System.Collections
Specialized namespace. It allows you to associate one string
key with multiple string
values.