HashTable In C# With Example

Overview

Today, we will see, how hashtables work in C#. Hashtables are nothing but the collection of the key-value pairs. We will see in detail in this blog, so let's start .

Introduction

Hashtable is a collection of the Key-Value pairs, which are organized on the hash code of their respective keys.

When you add an element, it gets added to the hashtable and its corresponding hash code is generated automatically. Here, we are using the keys to access those hashcodes. Hashtable optimizes lookup with the help of the keys.

Syntax

Now, we had created a constructor, using its default constructor.

  1. Hashtable at=new Hashtable();  
Add a Key
  1. at.Add(“1”,”Value”);  
Here 1 is the key with its respective values.

Now, you have to use for each loop to display the elements.

For retrieving the elements through the hashtable, use Dictionary Entry. 
  1. foreach(DictionaryEntry e in ht)   
  2. {  
  3.     Console.WriteLine(“{  
  4.         0  
  5.     }, {  
  6.         1  
  7.     }”, e.Key, e.Value);  
  8. }  
For checking keys
  1. ht.ContainsKey(1);  
For checking Value
  1. int value = (int) ht[“One”];   
We need to cast it as the data type

Properties of Hashtable 
  1. Keys: Gets an ICollection, which contains the keys in the Hashtable.
  2. Values: Gets an ICollection, which contains the values in the Hashtable.

Casting in Hashtable

We can use the ‘as’ operator to attempt to cast an object to a specific reference type. It returns true or false and you also reduce casting by using the ‘is’ operator.

If you want to store the keys in an array list: For storing the keys in an array list, you can use the keys property of Hashtable. For example,

  1. ArrayList al = new ArrayList(ht.Keys)  
  2.     // For retrieving elements in the arraylist  
  3. foreach(int key in al) r {  
  4.     Console.WriteLine(Key);  
  5. }  
Advantage of Hashtable 
  1. Hashtable allows the execution time for the lookup, retrieves and sets the operation to remain nearly constant, even for the large sets.
  2. In the large data sets, Hashtable is an ability to locate the item quickly.
  3. No need to scan through the entire data sets to find the items.

For example, we create an instance of Hashtable and added four elements in it. Subsequently, we are printing all the elements in the Hashtable and keys given to the array list for containing all the keys of Hashtable and finally printing it.

Example

  1. using System;  
  2. using System.Collections;  
  3. namespace hasthtab  
  4. {  
  5.     class Program   
  6.     {  
  7.         static void Main(string[] args) {  
  8.             Hashtable ht = new Hashtable();  
  9.             ht.Add("1""Akshay");  
  10.             ht.Add("2""Hari");  
  11.             ht.Add("3""Raghvan");  
  12.             ht.Add("4""Milind");  
  13.             ICollection key = ht.Keys;  
  14.             Console.WriteLine("Retrieving all elements: ");  
  15.             Console.WriteLine();  
  16.             foreach(var k in key) {  
  17.                 Console.WriteLine(k + ":" + ht[k]);  
  18.             }  
  19.             ArrayList al = new ArrayList(key);  
  20.             Console.WriteLine("Retrieving all keys in the arraylist");  
  21.             Console.WriteLine();  
  22.             foreach(var n in al) {  
  23.                 Console.WriteLine(n);  
  24.             }  
  25.             Console.ReadKey();  
  26.         }  
  27.     }  
  28. }  
Ebook Download
View all
Learn
View all