The NameValueCollection Class in C#

Introduction:
NameValueCollection allows many values for one key. It is found in System.Collections.Specialized. It does not provide excellent performance. Other collections are likely to be faster for your program.


NameValueCollection is used to store a collection of associated String keys and String values that can be accessed either with the key or with the index. It is very similar to C# HashTable, HashTable also stores data in Key , value format . NameValueCollection can hold multiple string values under a single key. As elements are added to a NameValueCollection, the capacity is automatically increased as required through reallocation.

Represents a collection of associated String keys and String values that can be accessed either with the key or with the index.

Inheritance Hierarchy
System.Object
  System.Collections.Specialized.NameObjectCollectionBase
    System.Collections.Specialized.NameValueCollection
      System.Net.WebHeaderCollection
      System.Web.HttpClientCertificate


Namespace:  System.Collections.Specialized
Assembly:  System (in System.dll)

Syntax:

[SerializableAttribute]
public class NameValueCollection : NameObjectCollectionBase

Adding new pairs syntax

  NameValueCollection.Add(name,value)
  NameValueCollection pair = new NameValueCollection();

  pair.Add("High", "80");

Get the value of corresponding Key syntax

  string[] NameValueCollection.GetValues(index);
  NameValueCollection pair = new NameValueCollection();
  pair.Add("High", "80");

  string[] vals = pair.GetValues(1);


Example:

using System;
using System.Collections.Specialized;

class Program
{
    static NameValueCollection GetCollection()
    {
 NameValueCollection collection = new NameValueCollection();
 collection.Add("A", "Dot Net Vepsh");
 collection.Add("B", "Microsoft");
 collection.Add("B", "USA");
 collection.Add("A", "UK");
 return collection;
    }

    static void Main()
    {
 NameValueCollection collection = GetCollection();
 foreach (string key in collection.AllKeys) // <-- No duplicates returned.
 {
     Console.WriteLine(key);
 }
    }
}

Output:
A

Get value:
Here we get a value in the collection using a string key. This is how you can use AppSettings in ASP.NET, or the QueryString collection. The collection returns null as a value if the key is not found.

If more than one value is found, the string returned contains the values joined by commas. 

Example: (C# program that uses NameValueCollection indexer)

using System;
using System.Collections.Specialized;

class Program
{
    static void Main()
    {
 NameValueCollection collection = GetCollection();
 Console.WriteLine(collection["A"]); // <-- Same as GetValues
 Console.WriteLine(collection["X"] == null); // <-- Not found
    }
}

Output:
Dot Net Vepsh, UK
True

Get methods:
Here we see that the NameValueCollection also allows you to test whether there are keys, with HasKeys. It also lets you use GetKey with an index to get the key at that index. You can use Get to get the value at that index. 

Example: (uses of 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
A
Dot Net Vepsh, UK
Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment