C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
GetCollection: This returns a new NameValueCollection. The collection maps two names (Sam, Bill) to four values.
So: In other words, two keys are mapped to two string arrays. The NameValueCollection always acts on strings.
String ArrayAlso: The NameValueCollection defines an AllKeys property. We use it in a foreach-loop to see all the keys in the collection.
PropertyForeachC# program that uses NameValueCollection
using System;
using System.Collections.Specialized;
class Program
{
static NameValueCollection GetCollection()
{
NameValueCollection collection = new NameValueCollection();
collection.Add("Sam", "The Dev Codes");
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);
}
}
}
Output
Sam
Bill
And: If more than one value is found, the string returned contains the values joined by commas.
C# program that uses NameValueCollection indexer
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
}
}
Output
The Dev Codes,IBM
True
Note: My research has shown that the fastest way of accessing values in small NameValueCollections is with these methods.
C# program that uses Get
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);
}
}
Output
True
Sam
The Dev Codes,IBM
Also: If you remove a key of NameValueCollection, all its values are also removed.
Note: To test this, add multiple identical keys, and then check the Count. Duplicates add to the result of Count.
Result: The NameValueCollection's lookup speed on even a small collection was poor. Its performance is likely much worse in many situations.
Therefore: The collection must be tested before being used in performance-critical code.
NameValueCollection tested
var collection = new NameValueCollection();
collection.Add("Sam", "The Dev Codes");
collection.Add("Bill", "Microsoft");
collection.Add("Steve", "Apple");
collection.Add("Rupert", "News Corporation");
Dictionary tested
var dictionary = new Dictionary<string, string>();
dictionary.Add("Sam", "The Dev Codes");
dictionary.Add("Bill", "Microsoft");
dictionary.Add("Steve", "Apple");
dictionary.Add("Rupert", "News Corporation");
NameValueCollection statement tested
string value = collection["Steve"];
Dictionary statement tested
string value = dictionary["Steve"];
Results: 10 million iterations
NameValueCollection lookup: 2768 ms
Dictionary lookup: 407 ms [faster]