Home
C#
NameValueCollection Usage
This page was last reviewed on Feb 27, 2024.
Dot Net Perls
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.
Dictionary
List
Example. 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.
String Literal
Part 1 We create a new NameValueCollection. The collection maps two keys to 4 values.
Part 2 We access keys by using the AllKeys property. We use it in a foreach-loop to see all the keys in the collection.
Property
foreach
Part 3 The collection returns null if the key is not found. If more than one value is found, it returns a string value joined by commas.
Part 4 With the HasKeys method, NameValueCollection can test whether there are keys.
bool
Part 5 GetKey with an index to gets the key at that index. This method always receives an int, not a string.
Part 6 Get returns the value at that index. This method also receives an 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
Benchmark. Does the NameValueCollection have competitive performance to the Dictionary? In some programs, we can use a NameValueCollection instead of a Dictionary.
Version 1 We access a value by passing a key to the NameValueCollection in this version.
Version 2 We use the Dictionary and access a value associated with a key. We use the same key as in the other version.
Result In 2024, 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
ASP.NET. 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.
Summary. NameValueCollection is located in the System.Collections Specialized namespace. It allows you to associate one string key with multiple string values.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 27, 2024 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.