Hash Table in C#

It's a collection type of object, which stores on the basis of KEY , VALUE.

Key is used to access the element and its value.

HashTable requires a pair of Key,Value.

If you know the ASP.NET, recollect or check VIEWSTATE declaration and recall mechanism.

Hash Table declaration and recall is similar to VIEWSTATE, SESSION.

System.Collections namespace is required to use HASHTABLE.

  1. using System.Collections;  

 

System.Collections

Hash Table

Represents a collection of Key/Value pairs that are organized based on the hash  code of the key.

You can assign the value to HASHTABLE with two different ways.

  1. Method 1 to Assign Key/Value to HashTable.
    1. Hashtable _htable = new Hashtable();  
    2. _htable["WindowsPath"] = "C:\\Windows";  
  2. Method 2 To Assign Key/Value to HashTable.
    1. Hashtable _htable = new Hashtable();  
    2. _htable.Add("WindowsPath""C:\\Windows");  

Step 1: Create Console Application.

Console Application

HashTable has the following method:

  1. Add: Add new item with Key and Value pair.

    Add

  2. Clear: Remove all items from HashTable.

    Clear

  3. Clone:

    Clone

  4. ContainsKey: Check if the given value exists in KEY side or not.

    ContainsKey

  5. ContainsValue: Check if the given value exists in VALUE side or not.

    ContainsValue

  6. CopyTo: Copy hashtable into array.

    CopyTo

  7. Keys: Hashtable stores value against title / heading of Keys.

    Keys

  8. Remove:

    Remove

  9. Values: Store the values of Keys.

    Values

  10. Count: Total numbers of keys/values pair in hashtable.

    Count

Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. namespace HashTableConsoleApp  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Hashtable _htable = new Hashtable();  
  14.             //Style 1 to add key/value  
  15.             _htable["1"] = "Ashish";  
  16.             //Style 2 to add key/value  
  17.             _htable.Add("2""Suhana");  
  18.             _htable.Add("3""Aakash");  
  19.             _htable.Add("4""Simran");  
  20.             _htable.Add("5""Rajesh");  
  21.             _htable.Add("6""Mahesh");  
  22.             _htable.Add("7""Suresh");  
  23.             _htable.Add("8""Pritesh");  
  24.             _htable.Add("9""Jayesh");  
  25.             _htable.Add("10""Mayuresh");  
  26.             _htable.Add("11""Ramesh");  
  27.             _htable.Add("12""Rajniesh");  
  28.             //To Get total numbers of item in hashtable.  
  29.             int TotalItems = _htable.Count;  
  30.             Console.WriteLine("=================================");  
  31.             Console.WriteLine("Displaying Total Numbers of Keys");  
  32.             Console.WriteLine("=================================");  
  33.             // Display the keys.  
  34.             foreach(string key in _htable.Keys)  
  35.             {  
  36.                 Console.WriteLine(key);  
  37.             }  
  38.             Console.WriteLine();  
  39.             Console.WriteLine();  
  40.             Console.WriteLine("==================================");  
  41.             Console.WriteLine("Displaying Total Numbers of Values");  
  42.             Console.WriteLine("==================================");  
  43.             // Display the values.  
  44.             foreach(string value in _htable.Values)  
  45.             {  
  46.                 Console.WriteLine(value);  
  47.             }  
  48.             Console.WriteLine("Total Item in HashTable :" + Convert.ToString(TotalItems));  
  49.             Console.WriteLine();  
  50.             Console.WriteLine();  
  51.             ///Removing 6 Number of Keys  
  52.             Console.WriteLine("==================================");  
  53.             Console.WriteLine("Removing 6th Item of HashTable");  
  54.             Console.WriteLine("==================================");  
  55.             _htable.Remove("6");  
  56.             Console.WriteLine("=================================");  
  57.             Console.WriteLine("Check 6th Number Keys Removed");  
  58.             Console.WriteLine("=================================");  
  59.             // Display the keys.  
  60.             foreach(string key in _htable.Keys)  
  61.             {  
  62.                 Console.WriteLine(key);  
  63.             }  
  64.             Console.WriteLine();  
  65.             Console.WriteLine();  
  66.             Console.Read();  
  67.             //To clear the hashtable.  
  68.             _htable.Clear();  
  69.         }  
  70.     }  
  71. }  
run

 

Ebook Download
View all
Learn
View all