HashTable

The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.
 
A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.
 
Hashtable defines many constructors, including these frequently used ones:

public Hashtable( )
public Hashtable(IDictionary d)
public Hashtable(int capacity)
public Hashtable(int capacity, fl oat loadFactor)

The first form constructs a default Hashtable. The second form initializes the Hashtable by using the elements of d. The third form initializes the capacity of the Hashtable to capacity. The fourth form initializes both the capacity and fill ratio. The fill ratio (also called the load factor) must be between 0.1 and 1.0, and it determines how full the hash table can be before it is resized upward. Specifically, when the number of elements is greater than the capacity of the table multiplied by its fill ratio, the table is expanded. For constructors that do not take a fill ratio, 1.0 is used.
 
In addition to the methods defined by the interfaces that it implements, Hashtable also defines several methods of its own. Some commonly used ones are shown in Following Table. To determine if a Hashtable contains a key, call ContainsKey( ). To see if a specific value is stored, call ContainsValue( ). To enumerate the contents of a Hashtable, obtain an IDictionaryEnumerator by calling GetEnumerator( ). Recall that DictionaryEnumerator is used to enumerate the contents of a collection that stores key/value pairs.

Method Description
public virtual bool ContainsKey(object key) Returns true if key is a key in the invoking Hashtable. Returns false otherwise.
public virtual bool ContainsValue(object value) Returns true if value is a value in the invoking Hashtable. Returns false otherwise.
public virtual IDictionaryEnumerator GetEnumerator( ) Returns an IDictionaryEnumerator for the invoking Hashtable.
public static Hashtable Synchronized(Hashtable table) Returns a synchronized version of the Hashtable passed in table.

Example

using System;
using System.IO;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            // Create a hash table.
            Hashtable ht = new Hashtable();
            // Add elements to the table.
            ht.Add("house", "Dwelling");
            ht.Add("car", "Means of transport");
            ht.Add("book", "Collection of printed words");
            ht.Add("apple", "Edible fruit");
            // Can also add by using the indexer.
            ht["tractor"] = "Farm implement";
            // Get a collection of the keys.
            ICollection c = ht.Keys;
            // Use the keys to obtain the values.
            foreach (string str in c)
                Console.WriteLine(str + ": " + ht[str]);
            Console.Read();
        }
    }
}