HashTable in C#


What is HashTable?


C# includes Hashtable collection in System.Collections namespace, which is similar to generic collection. The Hashtable collection stores key-value pairs. It optimizes lockups by computing the hash code of each key and stores it in a different bucket internally and then matches the hash code of the specified key at the time of accessing values.

Properties of Hashtable

Count: Gets the total count of key/value pairs in the Hashtable.
IsReadOnly: Gets boolean value indicating whether the Hashtable is read-only.
Item :Gets or sets the value associated with the specified key.
Keys :Gets an ICollection of keys in the Hashtable.
Values : Gets an ICollection of values in the Hashtable

Methods of HashTable

Add : The Add() method adds an item with a key and value into the Hashtable. Key and value can be of any data type. Key cannot be null whereas value can be null.

Example
static void Main(string[] args)
         {

            Hashtable ht = new Hashtable();
            ht.Add(1, "One");
            ht.Add(2, 2);
            ht.Add(3, 3.0);
            ht.Add(4, null);
        }

Remove: Removes the item with the specified key from the hashtable.

Example
static void Main(string[] args)
         {

            Hashtable ht = new Hashtable();
            ht.Add(1, "One");
            ht.Add(2, 2);
            ht.Add(3, 3.0);
            ht.Add(4, null);

            ht.Remove(1);
        }

Clear: Removes all the items from the hashtable.
Example

 static void Main(string[] args)
         {

            Hashtable ht = new Hashtable();
            ht.Add(1, "One");
            ht.Add(2, 2);
            ht.Add(3, 3.0);
            ht.Add(4, null);

            ht.Clear();
        }


Contains: Checks whether the hashtable contains a specific key.
Example

 static void Main(string[] args)
         {

            Hashtable ht = new Hashtable();
            ht.Add(1, "One");
            ht.Add(2, 2);
            ht.Add(3, 3.0);
            ht.Add(4, null);

            ht.Contains(1);//return true;
            ht.Contains(1);//return true;
            ht.Contains(5); //return false
        }

ContainsKey: Checks whether the hashtable contains a specific key it work similar as Contains.

ContainsValue: Checks whether the hashtable contains a specific value.

 static void Main(string[] args)
         {

            Hashtable ht = new Hashtable();
            ht.Add(1, "One");
            ht.Add(2, 2);
            ht.Add(3, 3.0);
            ht.Add(4, null);
            ht.ContainsValue(5); //return true
        }

GetHash: Returns the hash code for the specified key.




Summary:

  • Hashtable stores key-value pairs of any datatype where the Key must be unique.
  • The Hashtable key cannot be null whereas the value can be null.
  • Hashtable retrieves an item by comparing the hashcode of keys. So it is slower in performance than Dictionary collection.
  • Hashtable uses the default hashcode provider which is object.GetHashCode(). You can also use a custom hashcode provider.
  • Use DictionaryEntry with foreach statement to iterate Hashtable.


1 comment:

Please let me know if you have any feedback or doubts.

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...