Remove the element with the specified key from the Hashtable in C#

The Hashtable class in C# provides the Remove() method to delete elements with a specified key. This method removes the key-value pair from the hashtable and reduces the count by one.

Syntax

Following is the syntax for the Remove() method −

public virtual void Remove(object key)

Parameters

  • key − The key of the element to remove from the Hashtable.

Using Remove() to Delete Single Element

The following example demonstrates removing a single element from a Hashtable −

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable(10);
      hash.Add("1", "A");
      hash.Add("2", "B");
      hash.Add("3", "C");
      hash.Add("4", "D");
      hash.Add("5", "E");
      hash.Add("6", "F");
      hash.Add("7", "G");
      hash.Add("8", "H");
      hash.Add("9", "I");
      hash.Add("10", "J");
      
      Console.WriteLine("Original Hashtable Key and Value pairs...");
      foreach(DictionaryEntry entry in hash) {
         Console.WriteLine("{0} and {1}", entry.Key, entry.Value);
      }
      
      Console.WriteLine("Count of entries in Hashtable = " + hash.Count);
      hash.Remove("5");
      Console.WriteLine("Count of entries in Hashtable (after removal) = " + hash.Count);
   }
}

The output of the above code is −

Original Hashtable Key and Value pairs...
10 and J
1 and A
2 and B
3 and C
4 and D
5 and E
6 and F
7 and G
8 and H
9 and I
Count of entries in Hashtable = 10
Count of entries in Hashtable (after removal) = 9

Using Remove() to Delete Multiple Elements

The following example shows how to remove multiple elements by calling Remove() method multiple times −

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable(10);
      hash.Add("One", "Marketing");
      hash.Add("Two", "Operations");
      hash.Add("Three", "Finance");
      hash.Add("Four", "IT");
      hash.Add("Five", "Sales");
      hash.Add("Six", "Purchase");
      
      Console.WriteLine("Original Hashtable Key and Value pairs...");
      foreach(DictionaryEntry entry in hash) {
         Console.WriteLine("{0} and {1}", entry.Key, entry.Value);
      }
      
      Console.WriteLine("Count of entries in Hashtable = " + hash.Count);
      hash.Remove("Two");
      hash.Remove("Three");
      hash.Remove("Four");
      Console.WriteLine("Count of entries in Hashtable (after removals) = " + hash.Count);
      
      Console.WriteLine("Remaining elements:");
      foreach(DictionaryEntry entry in hash) {
         Console.WriteLine("{0} and {1}", entry.Key, entry.Value);
      }
   }
}

The output of the above code is −

Original Hashtable Key and Value pairs...
One and Marketing
Five and Sales
Three and Finance
Two and Operations
Four and IT
Six and Purchase
Count of entries in Hashtable = 6
Count of entries in Hashtable (after removals) = 3
Remaining elements:
One and Marketing
Five and Sales
Six and Purchase

How It Works

When you call the Remove() method:

  • It searches for the specified key in the hashtable
  • If the key is found, both the key and its corresponding value are removed
  • The Count property is decremented by one
  • If the key doesn't exist, no exception is thrown and no changes are made

Conclusion

The Remove() method in C# Hashtable efficiently deletes elements by their key. It's safe to use with non-existent keys and automatically updates the hashtable's count, making it ideal for dynamic data management scenarios.

Updated on: 2026-03-17T07:04:36+05:30

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements