Monday, August 18, 2008

Dictionary Object

Searching a dictionary object becomes very easy! You neednot put a forloop and look through rather use a foreach loop to check the keyvalue pair to find the exact match as shown below :

Dictionary Employee = new Dictionary();

if (Employee .TryGetValue("Jim", out value))

{

Console.WriteLine("For key = \"Employee\", value = {0}.", value);

else

Console.WriteLine("Key = \"Employee\" is not found.");

}


Contains can also be used to search through the dictionary obejct, but used mostly inserting a new value into the dictionary, it checks and then adds.

if (Employee .ContainsKey("Jim"))
{
flag = true; //search value found
}
else
flag = false;//search value not found





No comments: