NameValueCollection In C#.NET

Introduction: In this article we will see what is NameValueCollection  class provided by .NET and when to use this class.

Description:

1.       It’s a data structure that store data in Key(String) and Value(string) format.
2.       It allows user to store multiple(duplicate) values for the same Key.
3.       You can access data either via Key or via index.
4.       It’s very similar to HashTable that also allow you to store data in Key and Value format.
5.       When you add a value in the NameValueCollection , Its size grows automatically by relocation.
6.       This class is situated in System.Collections.Specialized namespace.
7.       Its performance is very poor than any other data structure like List, Dictionary or Hash Table so you must be very careful while choosing this data structure unless you tested it for performance.
8.       We cannot use NameValueCollection in situations where each Key has unique values.
9.       NameValueCollection is weakly type while Dictionary is strongly typed.
10.   Using a key we can get all the values for the single key.

Declaration: We declare NameValueCollection like this.

NameValueCollection nvc = new NameValueCollection { {“a”, “1”}, {“b”, “2”} };

OR

var nvc = new NameValueCollection { {“a”, “1”}, {“b”, “2”} };

Demo:

  • A method that returns a NameValueCollection .

public static NameValueCollection GetCollection()
{
NameValueCollection collection = new NameValueCollection();
collection.Add(“Lajapathy”, “:INQ”);
collection.Add(“partiban”, “WCF”);
collection.Add(“partiban”, “Silverlight”);
collection.Add(“Sathiya”, “C#”);
collection.Add(“Sathiya”, “dot net”);
collection.Add(“Sangita”, “C#”);
return collection;
}

  • To display the Unique keys from the collection.

public void DisplayOnlyUniqueKeys()
{
NameValueCollection collection = GetCollection();
// No duplicates returned.
foreach (string key in collection.AllKeys)
{
Response.Write(key + “,”);
}
}

  • To get the value of a key from the collection:public void DisplayValue()
    {
    NameValueCollection collection = GetCollection();
    Response.Write(string.Format(“Value :”, collection[“Sathiya”]));
    }
  • How to remove the values using the key:public void RemoveValue()
    {
    NameValueCollection collection = GetCollection();
    collection.Remove(“Lajapathy”);
    // No duplicates returned.
    foreach (string key in collection.AllKeys)
    {
    Response.Write(key + “,”);
    }
    }
  • How to clear the collection using the following approach:public void ClearValue()
    {
    NameValueCollection collection = GetCollection();
    collection.Clear();
    // No duplicates returned.
    foreach (string key in collection.AllKeys)
    {
    Response.Write(key + “,”);
    }
    }
  • How to get the values as an Array:public void GetDataArray()
    {
    NameValueCollection collection = GetCollection();
    string[] values = collection.GetValues(“partiban”);
    // No duplicates returned.
    foreach (string key in values)
    {
    Response.Write(key + “,”);
    }
    }
  • How to get a key using the index of the collection:public void GeKey()
    {
    NameValueCollection collection = GetCollection();
    string key = collection.GetKey(1);
    Response.Write(string.Format(“Value :”, key));
    }
  • Declaration of the above code snippet:protected void Page_Load(object sender, EventArgs e)
    {
    DisplayOnlyUniqueKeys();
    DisplayValue();
    ClearValue();
    GetDataArray();
    GeKey();
    }
  • To display the duplicate keys from the collection:public void DisplayAllKeys()
    {
    NameValueCollection collection = GetCollection();
    // No duplicates returned.
    foreach (string key in collection.Keys)
    {
    Response.Write(key + “,”);
    }
    }
  • How to get values using a specific index from the collection:public void GetValuesByIndex()
    {
    NameValueCollection collection = GetCollection();
    string[] indexArray = collection.GetValues(1);
    // No duplicates returned.
    foreach (string key in indexArray)
    {
    Response.Write(key + “,”);
    }
    }
  • How to update the existing value of key:This updates the “Lajapathy” key to value “SharePoint”. Old value replaced by new one./// <summary>
    /// Used to set value to COllection and update its value if key exists
    /// </summary>
    public void SetValue()
    {
    NameValueCollection collection = GetCollection();
    collection.Set(“Lajapathy”, “SharePoint”);
    foreach (string key in collection.AllKeys)
    {
    Response.Write(key + “,”);
    }
    }
  • How to check whether the collection is NULL or Empty://Check the collection is null or empty
    public void CheckHasKey()
    {
    NameValueCollection collection = GetCollection();
    bool hasKey = collection.HasKeys();
    Response.Write(hasKey);
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s