C# - SPLessons

How to use C# HashTable Class

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

How to use C# HashTable Class

How to use C# HashTable Class

Description :
Hi Friends, let me give you a intro about How to use C# HashTable Class. It is a collection with key and value as a pair. Object are organized based on the hash code of the key. It is easy to perform action like Add, Delete, Search, etc. using key as Index of the list.  

Namespace :
[csharp] using System.Collections; [/csharp]

Step1 :
Please open visual studio, right now i have visual studio 2013 i just opened my visual studio and click on new project.

Step2 :
In the new project window please click on templates visual c# and then click on windows  and on the right side panel just click on console application. Please give a name for the console application and click ok button.

Step3 :
If you see the visual studio will gives you a predefined template program  for us.

Step4 :
Lets, Build simple console application to Add, Delete, Find value with key, Find value with value and Display items from HashTable.

Main Method :
[csharp] class Program { public static Hashtable hashtable = new Hashtable(); public static Boolean flag = true; static void Main(string[] args) { while (flag) { Console.WriteLine("\n1. Insert \t\t2. Remove \n3. Find value with key \t4. Find value with value\n5. Display\t\t6. Exit"); Console.Write("Please enter your Choice: "); string choice = Console.ReadLine(); switch (choice) { case "1": hashtable = Insert(hashtable); break; case "2": hashtable = Remove(hashtable); break; case "3": FindValueUsingKey(hashtable); break; case "4": FindValueUsingValue(hashtable); break; case "5": Display(hashtable); break; case "6": flag = false; break; default: Console.WriteLine("Enter correct choice !!"); break; } } } [/csharp]

Output :

Insert :
[csharp] private static Hashtable Insert(Hashtable hashtable) { Console.Write("Please enter data into HashTable.\nKey : "); string key = Console.ReadLine(); Console.Write("Value : "); string value = Console.ReadLine(); hashtable.Add(key, value); Display(hashtable); return hashtable; } [/csharp]

Output :

Remove :
[csharp] private static Hashtable Remove(Hashtable hashtable) { Console.Write("Please enter data to delete from HashTable.\nKey : "); string key = Console.ReadLine(); hashtable.Remove(key); Display(hashtable); return hashtable; } [/csharp]

Output :

Finding Value using Key :
[csharp] private static void FindValueUsingKey(Hashtable hashtable) { Console.Write("Please enter data to find value in HashTable.\nKey : "); string key = Console.ReadLine(); Boolean flag = hashtable.ContainsKey(key); if(flag) { Console.WriteLine(key + " have a Value in HashTable."); } else { Console.WriteLine(key + " doesn't have any Value in HashTable."); } } [/csharp]

Output :

Finding Value using Value :
[csharp] private static void FindValueUsingValue(Hashtable hashtable) { Console.Write("Please enter data to find value in HashTable.\nValue : "); string value = Console.ReadLine(); Boolean flag = hashtable.ContainsValue(value); if (flag) { Console.WriteLine(value + " is there in HashTable."); } else { Console.WriteLine(value + " is not there in HashTable."); } } [/csharp]

Output :

Display :
[csharp] private static void Display(Hashtable hashtable) { var item = hashtable.GetEnumerator(); while(item.MoveNext()) { Console.WriteLine(item.Key + "\t: " + item.Value); } } [/csharp]

Output :

Other Function :
There are many other function which you can try.

Keys :
Gets an ICollection containing the keys in the Hashtable.

Values :
Gets an ICollection containing the values in the Hashtable.

Contains :
Determines whether the Hashtable contains a specific key.

CopyTo :
Returns an Hashtable to an existing one-dimensional Array.

Clear :
Removes all elements from the Hashtable. etc.

Enjoy to Code..;)