How to use C# HashTable Class

The Hashtable class in C# is a collection that represents a set of key/value pairs. It provides a mapping mechanism where keys are associated with corresponding values. This class is part of the System.Collections namespace.

With a Hashtable, you can store and retrieve data based on unique keys. Any non-null object can be used as a key, allowing for flexibility in the types of keys used. However, the values stored in the Hashtable can be of any type, including null.

Let's explore how to work with a Hashtable in C#:

First, import the System.Collections namespace:

using System.Collections;

Next, create an instance of the Hashtable class:

Hashtable myHashtable = new Hashtable();

To add key/value pairs to the Hashtable, you can use the Add() method:

myHashtable.Add("Name", "John"); myHashtable.Add("Age", 25); myHashtable.Add("IsStudent", true);

In this example, we added three key/value pairs to the Hashtable. Note that both keys and values are treated as objects.

To retrieve values from the Hashtable, you can use the indexer and provide the key:

string name = (string)myHashtable["Name"]; int age = (int)myHashtable["Age"]; bool isStudent = (bool)myHashtable["IsStudent"];

Since the values are stored as objects in the Hashtable, you need to explicitly cast them to the appropriate types when retrieving them.

C# Hashtable | useful methods and properties

Hashtable also provides several useful methods and properties. Here are a few examples:

ContainsKey: Checks if the Hashtable contains a specific key.
bool hasAgeKey = myHashtable.ContainsKey("Age");
ContainsValue: Checks if the Hashtable contains a specific value.
bool hasJohnValue = myHashtable.ContainsValue("John");
Remove: Removes the entry with the specified key from the Hashtable.
Count: Returns the number of key/value pairs in the Hashtable.
int count = myHashtable.Count;

The following source code shows some important operations in a HashTable

Full Source C#
using System; using System.Collections; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Hashtable weeks = new Hashtable(); weeks.Add("1", "SunDay"); weeks.Add("2", "MonDay"); weeks.Add("3", "TueDay"); weeks.Add("4", "WedDay"); weeks.Add("5", "ThuDay"); weeks.Add("6", "FriDay"); weeks.Add("7", "SatDay"); //Display a single Item MessageBox.Show(weeks["5"].ToString ()); //Search an Item if (weeks.ContainsValue("TueDay")) { MessageBox.Show("Find"); } else { MessageBox.Show("Not find"); } //remove an Item weeks.Remove("3"); //Display all key value pairs foreach (DictionaryEntry day in weeks ) { MessageBox.Show (day.Key + " - " + day.Value ); } } } }

When you execute this C# program it will add seven entries in the hashtable. The first message it will display the item 5. Then it check the value "TueDay" is existing or not . Next it remove the third item from HashTable. Finaly it displays all items exist in the HashTable.