In C#, Hashtable is a non-generic collection of key-value pair, using which we can save or retrieve data based on Keys. A HashTable is immutable and cannot have duplicate entries. The Hashtable Class implements ICollection.

A hashtable, also known as a hash map, is a data structure that allows for efficient storage and retrieval of key-value pairs. It uses a technique called hashing to map keys to an index in an underlying array.

Declare or Create Hashtable

The Hashtable class is generally found in the namespace called System.Collections. So to execute any of the examples, we have to add using System.Collections; to the source code. You can create Hashtable as below

// create a hashtable
Hashtable myHashTable = new Hashtable();

In the above example, we have just created or declared Hashtable, now we can add and retrieve values in Hashtable as below

        // create a hashtable
        Hashtable myHashtable = new Hashtable();


        // add items to hashtable
        myHashtable.Add("Name", "Vikram");
        myHashtable.Add("Age", 12);
        myHashtable.Add("Country", "India");

        //retrieve values from hashtable
        Console.WriteLine(myHashtable["Name"]);
      

As you can see from above example, we are adding values in Hashtable using .Add() method, while we can fetch values "Keys", myHashtable["Name"]

Here are the common methods of a hash table:

  • Add: Adds an element with the specified key and value in the hash table.
  • Clear: Removes all the elements in the hash table.
  • ContainsKey: Determined whether the hash table contains a specified key or not.
  • ContainsValue: Determined whether the hash table contains a specified value or not.
  • Remove("Key"): Removes element from Hash table based on Key provided.

Here is the complete example, which we can create considering above methods.

using System;
using System.Collections;

public class Program
{
    public static void Main()
    {
        // Create a new dictionary
        Hashtable hashtable = new Hashtable();

        // Add key-value pairs to the hashtable
        hashtable["apple"] = 5;
        hashtable["banana"] = 10;
        hashtable["orange"] = 7;

        // Access values using keys
        Console.WriteLine(hashtable["apple"]);   // Output: 5

        // Check if a key exists
        bool exists = hashtable.ContainsKey("banana");
        Console.WriteLine(exists);               // Output: True

        // Update the value for a key
        hashtable["banana"] = 15;
        Console.WriteLine(hashtable["banana"]);  // Output: 15

        // Remove a key-value pair
        hashtable.Remove("orange");

        // Iterate over all key-value pairs
        foreach(DictionaryEntry s in hashtable) {
		   Console.WriteLine(s.Key + " - "+ s.Value);
		}
    }
}

Here is the output

5
True
15
apple - 5
banana - 15

hashtable-in-C#-example

In the above example, I have provided example of how we can add or remove values in hashtable and how to loop Hashtable items using foreach.

You may also like to read:

Delete or Remove item in C# List

SingleOrDefault vs FirstOrDefault in C#

Tuples in C# with an example

Create or initialize List Tuple in C#

C# Dictionary

C# Hashset

Add values in C# array (Multiple ways)

Return Multiple values in C# (Various ways)