In this article, we are going to see the practical usage of a NameValueCollection.
Used Namespaces:
using System;
using System.Collections.Specialized;
Advantage:
- We can store duplicate keys either with different values or the same value.
- Using a key we can get all the values for the single key.
- Single Key, but multiple values
Disadvantage:
We can store and retrieve only string values.
Step 1:Used Namespaces:
using System;
using System.Collections.Specialized;
Step 2:Collection used to show the Demo:
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;
}
Step 3: 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 + ",");
}
}
Step 4: To get the value of a key from the collection:
public void DisplayValue()
{
NameValueCollection collection = GetCollection();
Response.Write(string.Format("Value :", collection["Sathiya"]));
}
Step 5: 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 + ",");
}
}
Step 6: 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 + ",");
}
}
Step 7: 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 + ",");
}
}
Step 8: 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));
}
Step 9: Declaration of the above code snippet:
protected void Page_Load(object sender, EventArgs e)
{
DisplayOnlyUniqueKeys();
DisplayValue();
ClearValue();
GetDataArray();
GeKey();
}
Step 10: 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 + ",");
}
}
Step 11: Declaration of the above code snippet:
protected void Page_Load(object sender, EventArgs e)
{
SetValue();
CheckHasKey();
DisplayAllKeys();
GetValuesByIndex();
}
Step 12: 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 + ",");
}
}
Step 13: 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 + ",");
}
}
Step 14: 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);
}
Thanks for reading this article. Have a nice day.

kalai romiPosted Dec 26, 2019, 11:42 PM
Nice article
İlhami YılmazPosted Dec 21, 2018, 3:28 PM
But how can i pass an array? lets say i have an array like string[] myMessage = new string[] {"test1","test2"}; when i pass it data.Add("message", myMessage); i am getting an error. any ideas?
Lajapathy ArunPosted Sep 7, 2012, 3:04 AM
it is available in the step 2
Jitendra SahuPosted Sep 7, 2012, 3:00 AM
please share the GetCollection method. thanks
Arjun PanwareditedPosted Apr 10, 2012, 11:58 PMEdited Apr 10, 2012, 11:59 PM
Hi, can you tell me about GetCollection method?
Deepak DwijPosted Apr 10, 2012, 7:15 PM
NameValueCollection is the very useful stuff about collections. keep posting more Lajapathy ! Can you please tell me about the generics collection with practical approach ?