Remove an item from a Hashtable in C#

The Hashtable class in C# provides the Remove() method to delete key-value pairs. The method takes the key as a parameter and removes the corresponding entry from the hashtable if it exists.

Syntax

Following is the syntax for removing an item from a Hashtable −

hashtable.Remove(key);

Parameters

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

Return Value

The Remove()void. If the specified key does not exist in the hashtable, no exception is thrown and no changes are made.

Hashtable Remove Operation Before Remove(3) Key: 1 ? Value: Jack Key: 3 ? Value: Ben Removed After Remove(3) Key: 1 ? Value: Jack Key: 2 ? Value: Henry

Using Remove() Method

Example

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        Hashtable h = new Hashtable();
        h.Add(1, "Jack");
        h.Add(2, "Henry");
        h.Add(3, "Ben");
        h.Add(4, "Chris");
        
        Console.WriteLine("Initial list:");
        foreach (var key in h.Keys) {
            Console.WriteLine("Key = {0}, Value = {1}", key, h[key]);
        }
        
        // removing an item
        h.Remove(3);
        Console.WriteLine("New list after removing an item:");
        foreach (var key in h.Keys) {
            Console.WriteLine("Key = {0}, Value = {1}", key, h[key]);
        }
    }
}

The output of the above code is −

Initial list:
Key = 4, Value = Chris
Key = 3, Value = Ben
Key = 2, Value = Henry
Key = 1, Value = Jack
New list after removing an item:
Key = 4, Value = Chris
Key = 2, Value = Henry
Key = 1, Value = Jack

Removing Non-Existent Keys

Example

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        Hashtable h = new Hashtable();
        h.Add(1, "Jack");
        h.Add(2, "Henry");
        
        Console.WriteLine("Count before removal: " + h.Count);
        
        // Try to remove non-existent key
        h.Remove(99);
        Console.WriteLine("Count after trying to remove non-existent key: " + h.Count);
        
        // Remove existing key
        h.Remove(1);
        Console.WriteLine("Count after removing existing key: " + h.Count);
    }
}

The output of the above code is −

Count before removal: 2
Count after trying to remove non-existent key: 2
Count after removing existing key: 1

Checking Key Existence Before Removal

Example

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        Hashtable h = new Hashtable();
        h.Add("name", "John");
        h.Add("age", 25);
        h.Add("city", "New York");
        
        string keyToRemove = "age";
        
        if (h.ContainsKey(keyToRemove)) {
            h.Remove(keyToRemove);
            Console.WriteLine("Key '{0}' removed successfully", keyToRemove);
        } else {
            Console.WriteLine("Key '{0}' not found", keyToRemove);
        }
        
        Console.WriteLine("Remaining items:");
        foreach (DictionaryEntry entry in h) {
            Console.WriteLine("Key = {0}, Value = {1}", entry.Key, entry.Value);
        }
    }
}

The output of the above code is −

Key 'age' removed successfully
Remaining items:
Key = city, Value = New York
Key = name, Value = John

Conclusion

The Remove() method provides a simple way to delete key-value pairs from a Hashtable in C#. It safely handles non-existent keys without throwing exceptions, making it reliable for removing items even when you're uncertain if the key exists.

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

817 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements