Hashtable In C#

In this article, you will learn about C# HashTable and how to use a HashTable in C#.

C# Hashtable class represents a hashtable in C#. A hash table is a collection of key/value pairs that are stored based on the hash code of the key in the collection. C# HashTable is a generic collection. It is similar to the generic dictionary collection included in the System.Collection.Generics namespace.
 
In this article, let's review C# HashTable class, HashTable class methods, and HashTable class properties and how to use them in our C# code.  
 
HashTable class has 16 different types of constructors, like Hashtable(), Hashtable(IDictionary), etc.
 

HashTable class properties

 
Here is a list of HashTable class properties,
 
Keys
Gets the keys contained in the Hashtable.
Values
Gets the Values contained in the Hashtable.
Count
Give the number of key/Value pairs contained in the Hashtable.
Item[Object]
Gets or sets the value for the particular key. (object=key)
IsReadOnly
Gets bool value the Hashtable is read-only.
IsFixedSize
Gets bool value the Hashtable is fixed size.
 

HashTable class methods

 
Here is a list of HashTable class methods,
 
Add(Object, Object)
Adds item with the specified key and value into the Hashtable.
Clear()
Removes all items from the Hashtable.
Clone()
Creates a shallow copy of the Hashtable.
Contains(Object)
Returns bool value, determines whether the Hashtable contains a specific key.
ContainsKey(Object)
Returns bool value, determines whether the Hashtable contains a specific key.
ContainsValue(Object)
Returns bool value, determines whether the Hashtable contains a specific value.
KeyEquals(Object, Object)
Returns bool value, compares a specific Object with a specific key in the Hashtable.
 
Example - HashTable Initialization 
 
The following code snippet creates a HashTable where the value type is a string.
  1. Hashtable ht = new Hashtable();  
Example 1 - Add elements to a HashTable
 
The following code snippet adds items to a HashTable collection.
  1. NumberHashtable.Add(1, "One");
  2. NumberHashtable.Add(2, "Two");  
  3. NumberHashtable.Add(3, "Three");  
  4. Console.WriteLine("--Number Hashtable--");  
  5. //The Hashtable is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.  
  6. foreach (DictionaryEntry item in NumberHashtable)  
  7. {  
  8.    Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");  
  9. }  
Output
 
C# - Hashtable
 
Example 2
 
The following code snippet creates a HashTable where the value type is List<string>.
  1. Hashtable ContryNameHashtable = new Hashtable();  
  2.   
  3.  //The following code snippet creates a list where collection of contry name alphabet wise  
  4.  List<string> ContryNameStartWith_A = new List<string> { "Afghanistan""Albania""Algeria""Andorra" };  
  5.  List<string> ContryNameStartWith_B = new List<string> { "Bahamas""Bahrain""Bangladesh""Barbados" };  
  6.  List<string> ContryNameStartWith_C = new List<string> { "Cambodia""Cameroon""Canada""Cape Verde" };  
  7.   
  8.  // The following code snippet adds items to the Hashtable.   
  9.  ContryNameHashtable.Add('A', ContryNameStartWith_A);  
  10.  ContryNameHashtable.Add('B', ContryNameStartWith_B);  
  11.  ContryNameHashtable.Add('C', ContryNameStartWith_C);  
  12.  Console.WriteLine("--Alphabet Wise Country Name--");  
  13.  //The Hashtable is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.  
  14.  foreach (DictionaryEntry item in ContryNameHashtable)  
  15.  {  
  16.     List<string> countrysName = ContryNameHashtable[item.Key] as List<string>;  
  17.     Console.WriteLine($"Alphabet: {item.Key}, --> Contry Name: {string.Join("", countrysName)}");  
  18.  }  
Output
 
C# - Hashtable
 
Example 3
  1. public class StudentInfo  
  2. {  
  3.       public string Name { get; set; }  
  4.       public string Gender { get; set; }  
  5.       public string Standard { get; set; }  
  6.       public int Total { get; set; }  
  7. }  
The following code snippet creates a HashTable where the key type is int (Student Roll Number) and the value type is user-defined class StudentInfo.
  1. Hashtable StudentInfoHashtable = new Hashtable();  
  2.   
  3. //The following code snippet creates a list where the collection of student information.  
  4. StudentInfo student1 = new StudentInfo() { Name = "Emma", Gender = "Female", Standard = "10", Total = 506 };  
  5. StudentInfo student2 = new StudentInfo() { Name = "Liam", Gender = "Male", Standard = "12", Total = 600 };  
  6. StudentInfo student3 = new StudentInfo() { Name = "Noah", Gender = "Male", Standard = "09", Total = 550 };  
  7. StudentInfo student4 = new StudentInfo() { Name = "Olivia", Gender = "Female", Standard = "08", Total = 685 };  
  8. StudentInfo student5 = new StudentInfo() { Name = "Ava", Gender = "Female", Standard = "11", Total = 605 };  
  9. StudentInfo student6 = new StudentInfo() { Name = "Oliver", Gender = "Male", Standard = "10", Total = 700 };  
  10.   
  11. // The following code snippet adds items to the Hashtable.   
  12. StudentInfoHashtable.Add(1, student1);  
  13. StudentInfoHashtable.Add(2, student2);  
  14. StudentInfoHashtable.Add(3, student3);  
  15. StudentInfoHashtable.Add(4, student4);  
  16. StudentInfoHashtable.Add(5, student5);  
  17. StudentInfoHashtable.Add(6, student6);  
  18.   
  19. Console.WriteLine("--Student Information Hashtable--");  
  20.   
  21. Console.WriteLine("-----------Student Information-----------");  
  22.   
  23. //The Hashtable is a collection. We can use the foreach loop to go through all the items and read them using the Key and Value properties.  
  24. foreach (DictionaryEntry item in StudentInfoHashtable)  
  25. {  
  26.    StudentInfo StudentInfo = StudentInfoHashtable[item.Key] as StudentInfo;  
  27.   
  28.    Console.WriteLine($"Roll No: {item.Key}");  
  29.    Console.Write($"Name: {StudentInfo.Name}");  
  30.    Console.Write($", Gender: {StudentInfo.Gender}");  
  31.    Console.Write($", Standard: {StudentInfo.Standard}");  
  32.    Console.WriteLine($", Total: {StudentInfo.Total}");  
  33.    Console.WriteLine("-------------------------------------------------------");  
  34. }  
Output
 
C# - Hashtable
 
Example - Clone a HashTable
 
The following code snippet clones the HashTable from "NumberHashtable" to "cloneHashtable".
  1. Hashtable cloneHashtable = NumberHashtable.Clone() as Hashtable;  
  2.   
  3. Console.WriteLine("--Example of Clone Hashtable--");  
  4. Console.WriteLine("--Number Hashtable--");  
  5. //The Hashtable is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.  
  6. foreach (DictionaryEntry item in NumberHashtable)  
  7. {  
  8.    Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");  
  9. }  
  10. Console.WriteLine("--Clone Number Hashtable--");  
  11. //The Hashtable is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.  
  12. foreach (DictionaryEntry item in cloneHashtable)  
  13. {  
  14.    Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");  
  15. }  
Output
 
C# - Hashtable
 
Example - Contains() 
 
The following code snippet checks if the Key/Value is present in Number HashTable.
  1. Console.WriteLine("--Example of Contains()--");  
  2. Console.WriteLine($"Is the key 'One' present in NumberHashtable?: {NumberHashtable.Contains("Ten")}");  
  3. Console.WriteLine($"Is the key '1' present in NumberHashtable?: {NumberHashtable.Contains(1)}");  
  4. Console.WriteLine("--Example of ContainsKey()--");  
  5. Console.WriteLine($"Is the key '1' present in NumberHashtable?: {NumberHashtable.ContainsKey(1)}");  
  6. Console.WriteLine($"Is the key '10' present in NumberHashtable?: {NumberHashtable.ContainsKey(10)}");  
  7. Console.WriteLine("--Example of ContainsValue()--");  
  8. Console.WriteLine($"Is the value 'Ten' present in NumberHashtable?: {NumberHashtable.ContainsValue("Ten")}");  
  9. Console.WriteLine($"Is the value 'One' present in NumberHashtable?: {NumberHashtable.ContainsValue("One")}");  
Output
 
C# - Hashtable
 
Example - synchronized()
 
The following code snippet synchronizes the Hashtable from "NumberHashtable" to "synchronizedNumberHashtable".
  1. Hashtable synchronizedNumberHashtable = Hashtable.Synchronized(NumberHashtable);  
  2.   
  3. // print the status of both Hashtables   
  4. Console.WriteLine("NumberHashtable Hashtable is {0}.",NumberHashtable.IsSynchronized ? "synchronized" : "not synchronized");  
  5.   
  6. Console.WriteLine("synchronizedNumberHashtable Hashtable is {0}.",synchronizedNumberHashtable.IsSynchronized ? "synchronized" : "not synchronized");  
Output
 
C# - Hashtable
 
Example - Count and Clear()
  
The following code snippet counts and makes clear() the HashTable.
  1. Console.WriteLine($"Before 'NumberHashtable.Clear()' Number of items in NumberHashtable= {NumberHashtable.Count}");  
  2. NumberHashtable.Clear();  
  3. Console.WriteLine($"After 'NumberHashtable.Clear()' Number of items in NumberHashtable= {NumberHashtable.Count}");  
Output
 
C# - Hashtable
 

Summary

 
In this article, we learned about HashTable in C# and how to use its methods and properties.