Tuesday, August 12, 2008

NameValueCollection Class

Defination : NameValueCollection Class is collection of keys and values that can be accessed either with keys or by index.

Namespace : System.Collections.Specialized

Description: It can able to store multiple string values under single key

Below Code Snippet will describe this clearly.

NameValueCollection keyValue = new NameValueCollection();
keyValue.Add("A","Apples");
keyValue.Add("B","Banana");
keyValue.Add("C","Cat") ;
keyValue.Add("A","Ant");

Response.Write("Key Value\n");
foreach(string strNameValue in keyValue.AllKeys)
{
Response.Write(strNameValue + " " + keyValue[strNameValue]);
Response.Write("\n");
}

//Output will be
Key Value
A Apple,Ant
B Banana
C Cat

Hope this article will help you

Kumar