Home
Map
NameValueCollection UsageUse the NameValueCollection type and benchmark its lookup performance.
C#
This page was last reviewed on Sep 27, 2022.
NameValueCollection. This C# type allows many values for one key. It is found in System.Collections.Specialized. It does not provide excellent performance.
Type notes. Other collections are likely to be faster than NameValueCollection. Unless you need to use NameValueCollection, you should consider Dictionary or 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
Detail This returns a new NameValueCollection. The collection maps two names (Sam, Bill) to 4 values.
So In other words, two keys are mapped to two string arrays. The NameValueCollection always acts on strings.
Array
Also The NameValueCollection defines an AllKeys property. We use it in a foreach-loop to see all the keys in the collection.
Property
Foreach
using System; using System.Collections.Specialized; class Program { static NameValueCollection GetCollection() { NameValueCollection collection = new NameValueCollection(); collection.Add("Sam", "Dot Net Perls"); collection.Add("Bill", "Microsoft"); collection.Add("Bill", "White House"); collection.Add("Sam", "IBM"); return collection; } static void Main() { NameValueCollection collection = GetCollection(); foreach (string key in collection.AllKeys) // <-- No duplicates returned. { Console.WriteLine(key); } } }
Sam Bill
Get value. We pass a string to Get. The collection returns null as a value if the key is not found. If more than one value is found, it returns a string value joined by commas.
using System; using System.Collections.Specialized; class Program { static void Main() { NameValueCollection collection = GetCollection(); Console.WriteLine(collection["Sam"]); // <-- Same as GetValues Console.WriteLine(collection["X"] == null); // <-- Not found } }
Dot Net Perls,IBM True
HasKeys. NameValueCollection can test whether there are keys, with HasKeys. GetKey with an index to gets the key at that index. And Get returns the value at that index.
Note My research has shown that the fastest way of accessing values in small NameValueCollections is with these methods.
using System; using System.Collections.Specialized; class Program { static void Main() { NameValueCollection collection = GetCollection(); // <-- See first example // Write whether the collection has keys. Console.WriteLine(collection.HasKeys()); // Write the first key. Console.WriteLine(collection.GetKey(0)); // Get the first value. string value = collection.Get(0); Console.WriteLine(value); } }
True Sam Dot Net Perls,IBM
Benchmark. Here we see sample code that was benchmarked. We can paste the code blocks into a test harness and compile in Release mode outside of the debugger.
Result The NameValueCollection's lookup speed on even a small collection was poor. Its performance is likely much worse in many situations.
Detail The collection must be tested before being used in performance-critical code.
var collection = new NameValueCollection(); collection.Add("Sam", "Dot Net Perls"); collection.Add("Bill", "Microsoft"); collection.Add("Steve", "Apple"); collection.Add("Rupert", "News Corporation");
var dictionary = new Dictionary<string, string>(); dictionary.Add("Sam", "Dot Net Perls"); dictionary.Add("Bill", "Microsoft"); dictionary.Add("Steve", "Apple"); dictionary.Add("Rupert", "News Corporation");
string value = collection["Steve"];
string value = dictionary["Steve"];
NameValueCollection lookup: 2768 ms Dictionary lookup: 407 ms [faster]
Remove value. You can also remove a key-value pair using the Remove method on NameValueCollection. This method requires that you pass it a string key for the pair.
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.
And The NameValueCollection is also used in the query string collection in ASP.NET.
Summary. NameValueCollection is located in the System.Collections.Specialized namespace. It allows you to associate one string key with multiple string values.
C#VB.NETPythonGolangJavaSwiftRust
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 Sep 27, 2022 (edit).
Home
Changes
© 2007-2023 Sam Allen.