Introduction
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 the C# HashTable class, HashTable class methods, and HashTable class properties and how to use them in our C# code.
The 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.
Hashtable ht = new Hashtable();
Example 1. Add elements to a HashTable
The following code snippet adds items to a HashTable collection.
NumberHashtable.Add(1, "One");
NumberHashtable.Add(2, "Two");
NumberHashtable.Add(3, "Three");
Console.WriteLine("--Number Hashtable--");
// The Hashtable is a collection. We can use the foreach loop to go through all the items and read them using their Key and Value properties.
foreach (DictionaryEntry item in NumberHashtable)
{
Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
}
Output
Example 2. The following code snippet creates a HashTable where the value type is List<string>.
Hashtable ContryNameHashtable = new Hashtable();
// The following code snippet creates lists of countries alphabetically.
List<string> ContryNameStartWith_A = new List<string> { "Afghanistan", "Albania", "Algeria", "Andorra" };
List<string> ContryNameStartWith_B = new List<string> { "Bahamas", "Bahrain", "Bangladesh", "Barbados" };
List<string> ContryNameStartWith_C = new List<string> { "Cambodia", "Cameroon", "Canada", "Cape Verde" };
// The following code snippet adds items to the Hashtable.
ContryNameHashtable.Add('A', ContryNameStartWith_A);
ContryNameHashtable.Add('B', ContryNameStartWith_B);
ContryNameHashtable.Add('C', ContryNameStartWith_C);
Console.WriteLine("--Alphabet Wise Country Name--");
// The Hashtable is a collection. We can use the foreach loop to go through all the items and read them using their Key and Value properties.
foreach (DictionaryEntry item in ContryNameHashtable)
{
List<string> countrysName = item.Value as List<string>;
Console.WriteLine($"Alphabet: {item.Key}, --> Country Name: {string.Join(", ", countrysName)}");
}
Output
Example 3
public class StudentInfo
{
public string Name { get; set; }
public string Gender { get; set; }
public string Standard { get; set; }
public int Total { get; set; }
}
The following code snippet creates a HashTable where the key type is int (Student Roll Number) and the value type is the user-defined class StudentInfo.
Hashtable StudentInfoHashtable = new Hashtable();
// The following code snippet creates a list where the collection of student information.
StudentInfo student1 = new StudentInfo() { Name = "Emma", Gender = "Female", Standard = "10", Total = 506 };
StudentInfo student2 = new StudentInfo() { Name = "Liam", Gender = "Male", Standard = "12", Total = 600 };
StudentInfo student3 = new StudentInfo() { Name = "Noah", Gender = "Male", Standard = "09", Total = 550 };
StudentInfo student4 = new StudentInfo() { Name = "Olivia", Gender = "Female", Standard = "08", Total = 685 };
StudentInfo student5 = new StudentInfo() { Name = "Ava", Gender = "Female", Standard = "11", Total = 605 };
StudentInfo student6 = new StudentInfo() { Name = "Oliver", Gender = "Male", Standard = "10", Total = 700 };
// The following code snippet adds items to the Hashtable.
StudentInfoHashtable.Add(1, student1);
StudentInfoHashtable.Add(2, student2);
StudentInfoHashtable.Add(3, student3);
StudentInfoHashtable.Add(4, student4);
StudentInfoHashtable.Add(5, student5);
StudentInfoHashtable.Add(6, student6);
Console.WriteLine("--Student Information Hashtable--");
Console.WriteLine("-----------Student Information-----------");
// 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.
foreach (DictionaryEntry item in StudentInfoHashtable)
{
StudentInfo studentInfo = StudentInfoHashtable[item.Key] as StudentInfo;
Console.WriteLine($"Roll No: {item.Key}");
Console.Write($"Name: {studentInfo.Name}");
Console.Write($", Gender: {studentInfo.Gender}");
Console.Write($", Standard: {studentInfo.Standard}");
Console.WriteLine($", Total: {studentInfo.Total}");
Console.WriteLine("-------------------------------------------------------");
}
Output
Example. Clone a HashTable
The following code snippet clones the HashTable from "NumberHashtable" to "cloneHashtable".
Hashtable cloneHashtable = (Hashtable)NumberHashtable.Clone();
Console.WriteLine("--Example of Clone Hashtable--");
Console.WriteLine("--Number Hashtable--");
// 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.
foreach (DictionaryEntry item in NumberHashtable)
{
Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
}
Console.WriteLine("--Clone Number Hashtable--");
// 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.
foreach (DictionaryEntry item in cloneHashtable)
{
Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
}
Output
Example. Contains()
The following code snippet checks if the Key/Value is present in Number HashTable.
Console.WriteLine("--Example of Contains()--");
Console.WriteLine($"Is the key 'One' present in NumberHashtable?: {NumberHashtable.Contains("Ten")}");
Console.WriteLine($"Is the key '1' present in NumberHashtable?: {NumberHashtable.Contains(1)}");
Console.WriteLine("--Example of ContainsKey()--");
Console.WriteLine($"Is the key '1' present in NumberHashtable?: {NumberHashtable.ContainsKey(1)}");
Console.WriteLine($"Is the key '10' present in NumberHashtable?: {NumberHashtable.ContainsKey(10)}");
Console.WriteLine("--Example of ContainsValue()--");
Console.WriteLine($"Is the value 'Ten' present in NumberHashtable?: {NumberHashtable.ContainsValue("Ten")}");
Console.WriteLine($"Is the value 'One' present in NumberHashtable?: {NumberHashtable.ContainsValue("One")}");
Output
Example. synchronized()
The following code snippet synchronizes the Hashtable from "NumberHashtable" to "synchronizedNumberHashtable".
Hashtable synchronizedNumberHashtable = Hashtable.Synchronized(NumberHashtable);
// print the status of both Hashtables
Console.WriteLine("NumberHashtable Hashtable is {0}.", NumberHashtable.IsSynchronized ? "synchronized" : "not synchronized");
Console.WriteLine("synchronizedNumberHashtable Hashtable is {0}.", synchronizedNumberHashtable.IsSynchronized ? "synchronized" : "not synchronized");
Output
Example. Count and Clear()
The following code snippet counts and makes clear() the HashTable.
Console.WriteLine($"Before 'NumberHashtable.Clear()' Number of items in NumberHashtable= {NumberHashtable.Count}");
NumberHashtable.Clear();
Console.WriteLine($"After 'NumberHashtable.Clear()' Number of items in NumberHashtable= {NumberHashtable.Count}");
Output
Summary
In this article, we learned about HashTable in C# and how to use its methods and properties.