Wednesday 1 February 2012

Hashtable in .Net


Hashtable Concept in .Net


Provides a collection of key-value pairs that are organized
based on the hash code of the key.

Example:
static void Main()
{
    Hashtable ht =  new Hashtable(20);
    ht.Add("ht01", "DotNetGuts");
    ht.Add("ht02", "EasyTutor.2ya.com");
    ht.Add("ht03", "DailyFreeCode.com");
   
    Console.WriteLine("Printing Keys...");
    foreach(string key in ht.Keys)
    {
        Console.WriteLine(key);
    }
   
    Console.WriteLine("\nPrinting Values...");
    foreach(string Value in ht.Values)
    {
        Console.WriteLine(Value);
    }
   
    Console.WriteLine("Size of Hashtable is {0}", ht.Count);
   
    Console.WriteLine(ht.ContainsKey("ht01"));
    Console.WriteLine(ht.ContainsValue("DailyFreeCode.com"));
   
    Console.WriteLine("\nRemoving element with key = ht02");
    ht.Remove("ht02");
   
    Console.WriteLine("Size of Hashtable is {0}", ht.Count);
   
}

Output

Printing Keys...
ht01
ht02
ht03

Printing Values...
DotNetGuts
EasyTutor.2ya.com
DailyFreeCode.com
Size of Hashtable is 3
True
True
 
Removing element with key = ht02
Size of Hashtable is 2

No comments:

Post a Comment