HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

HashTable Collection Type
Hashtable is a collection class prsent in System.Collections namespace. The data in a Hashtable is stored as key-value pairs. For example if we have a list of PersonId's and Names. PersonId's can be stored as a keys and Names can be stored as values. So each element in the Hashtable will have the personID as the key and Name as value. As data in the Hashtable is stored as key-value pairs searching for a specific person Name by key will be very fast.

The following example shows how to create and initialize a Hashtable and how to print out its keys and values.

DictionaryEntry is a struct whcih can hold a HashTable elements key and Value. In our example we are using DictonaryEntry to loop thru the HashTable.

Elements in a HashTable cannot be sorted. To sort items alphabetically or numerically, use the SortedList object.

using System;

using System.Collections;

class HashTableDemo

{

    public static void Main()

    {

        //Create an instance of HashTable

        Hashtable HT = new Hashtable();

        //Add elements(Key-Value pairs) to the HashTable

        HT.Add(1, "Prasad");

        HT.Add(2, "Giri");

        HT.Add(3, "Ravi");

        //Print the total number of elements in the Hashtable using the count property

        Console.WriteLine("Total elements in HashTable = " + HT.Count.ToString());

        //Loop thru each dictonaryentry and print key and value of each element

        foreach (DictionaryEntry entry in HT)

        {

            Console.WriteLine("Key = " + entry.Key.ToString() + " Value = " + entry.Value.ToString());

        }

    }

}

Getting/Setting values using keys in a HashTable
HashTables are very fast in searching for a value when we provide the key. In the example below
  • We retrieve the Name of the Person whose key is 2
       Console.WriteLine(HT[2]);
  • We also set the value to Giridhar using the key
       HT[2] = "GiriDhar";

using System;

using System.Collections;

class HashTableDemo

{

    public static void Main()

    {

        //Create an instance of HashTable

        Hashtable HT = new Hashtable();

        //Add elements(Key-Value pairs) to the HashTable

        HT.Add(1, "Prasad");

        HT.Add(2, "Giri");

        HT.Add(3, "Ravi");

        //Print the Person Name with PersonID 2

        Console.WriteLine(HT[2]);

        //Change the Person Name from Giri to GiriDhar

        HT[2] = "GiriDhar";

        Console.WriteLine(HT[2]);

    }

}

Other HashTable useful methods with an example
  • ContainsKey()
  • ContainsValue()
  • Remove()
  • Clear()

using System;

using System.Collections;

class HashTableDemo

{

    public static void Main()

    {

        //Create an instance of HashTable

        Hashtable HT = new Hashtable();

        //Add elements(Key-Value pairs) to the HashTable

        HT.Add(1, "Prasad");

        HT.Add(2, "Giri");

        HT.Add(3, "Ravi");

 

        //Determines whether the Hashtable contains a specific key

        Console.WriteLine("Is key 2 Present:"+ HT.ContainsKey(2).ToString());

        //Determines whether the Hashtable contains a specific value

        Console.WriteLine("Is value Giri Present:" + HT.ContainsValue("Giri"));

        //Removes the element with the specified key from the Hashtable

        HT.Remove(2);

        //Removes all elements from the Hashtable.

        HT.Clear();

    }

}

Other related articles

Njoy Programming
   ByPrasad Cherukuri