How to Delete Item from Hashtable Collection in C#?


The Hashtable in C# is a collection of key−value pairs that are organized based on the hash code of the key. The items in the hashtable are accessed using a key. The Hashtable class of C# is the class that implements the hashtable.

Using this class, we can create a new hashtable object with the help of the constructors provided. The Hashtable class also provides various methods using which we can perform various operations on the hashtable. These operations include adding items, checking for the existence of specified keys, counting the number of items, removing an item from the hashtable, and so on.

In this article, we will discuss deleting an item from the hashtable collection given a specified key.

How to Delete Item from Hashtable Collection?

The Hashtable class provides a method “Remove” to remove/delete an item from the hashtable collection. Given a key, the Remove method will delete an item with the specified key from the hashtable.

The prototype of the Remove method is given below.

Syntax

public virtual void Remove (object key);

Parameters

  • Key − The key of the element that is to be removed from the hashtable collection. This is of type System.Object.

Implements

Remove(Object) method of the IDictionary interface.

Exceptions

  • ArgumentNullException − if the specified key is null this exception is thrown.

  • NotSupportedException − thrown if the hashtable has a fixed size or is read-only.

The “Remove()” method does not throw any exception if the specified key is not present in the hashtable. The hashtable will be unchanged if the key is not present and the program will succeed.

Programming Examples of Remove() method

Example 1

The following program demonstrates the use of the Remove() method to delete an item from the hashtable collection.

using System; using System.Collections; class MyHashTable { public static void Main(){ // Creating a Hashtable Hashtable numberNames = new Hashtable(); // Adding elements in Hashtable numberNames.Add("2", "Two"); numberNames.Add("13", "Thirteen"); numberNames.Add("24", "Twenty Four"); numberNames.Add("59", "Fifty Nine"); // Print the contents of Hashtable Console.WriteLine("**********Contents of Hashtable**********"); foreach(var item in numberNames.Keys){ Console.WriteLine("key ={0}, Value = {1}", item,numberNames[item]); } //read the key for which element is to be deleted Console.WriteLine("
Enter the key for which the element is to be removed from Hashtable "
); string key = (string)Console.ReadLine(); //remove the element numberNames.Remove(key); //display the hashtable after deletion Console.WriteLine("
******Contents of Hashtable(after deletion)******"
); foreach(var item in numberNames.Keys){ Console.WriteLine("key ={0}, Value = {1}", item,numberNames[item]); } } }

In this program, first, we have created a hashtable with Numbers as keys and their corresponding number names as the values. The hashtable is then displayed on the screen. Next, the user is prompted to enter the key whose element is to be deleted from the hashtable. Once the key is entered, the Remove() method is called with this key as an argument. Next, the hashtable contents are again displayed. If the key is present in the hashtable, the Remove() method will remove the element, otherwise, the hashtable will remain unchanged.

Output

The program generates the following output.

**********Contents of Hashtable**********
key =59, Value = Fifty Nine
key =24, Value = Twenty Four
key =13, Value = Thirteen
key =2, Value = Two

Enter the key for which the element is to be removed from Hashtable 
13
******Contents of Hashtable(after deletion)******
key =59, Value = Fifty Nine
key =24, Value = Twenty Four
key =2, Value = Two

The above output shows the difference between the hashtable contents before and after deletion.

Now let’s see how the output changes when the key entered by the user does not exist in the hashtable. In this case, as already mentioned, the hashtable remains unchanged and no exception is thrown. Here is the output generated by the above program.

Output

**********Contents of Hashtable**********
key =59, Value = Fifty Nine
key =24, Value = Twenty Four
key =13, Value = Thirteen
key =2, Value = Two

Enter the key for which the element is to be removed from Hashtable 
3
******Contents of Hashtable(after deletion)******
key =59, Value = Fifty Nine
key =24, Value = Twenty Four
key =13, Value = Thirteen
key =2, Value = Two

Here, the user has entered key = 3 which is not present in the hashtable. In this case, the hashtable remains unchanged as no element is deleted by the Remove() method.

Example 2

Now let’s consider another example of hashtable deletion. The program for the same is given below.

using System; using System.Collections; public class myHashtable{ public static void Main(){ // Create a new Hashtable. var tongueTwister = new Hashtable(); tongueTwister.Add("1a", "She"); tongueTwister.Add("1b", "sells"); tongueTwister.Add("1c", "sea"); tongueTwister.Add("2a", "shells"); tongueTwister.Add("2b", "on"); tongueTwister.Add("2c", "the"); tongueTwister.Add("3a", "sea"); tongueTwister.Add("3b", "shore"); // Displays the Hashtable. Console.WriteLine("The Hashtable initially contains the following:
"
); foreach (DictionaryEntry elem in tongueTwister) Console.WriteLine($" {elem.Key}: {elem.Value}"); Console.WriteLine(); // Removes the element with the specified key. string key =3b”; tongueTwister.Remove(key); // Displays the Hashtable after deletion. Console.WriteLine("
Hashtable after removing the key = "
{0}":",key); foreach (DictionaryEntry elem in tonguetwister) Console.WriteLine($" {elem.Key}: {elem.Value}"); Console.WriteLine(); } }

In this program, we have a hashtable that contains a tongue-twister “She sells sea shells on the seashore”. We have numbered the keys as 1a, 1b, 1c, 2a, 2b, and so on. First, we have displayed the entire hashtable. Then we use the Remove() method and remove the element with key = 3b. The newly updated hashtable is displayed again.

Output

The program generates the following output.

The Hashtable initially contains the following:
    3b:    shore
    1a:    She
    1b:    sells
    2b:    on
    2c:    the
    3a:    sea
    2a:    shells
    1c:    sea

Hashtable after removing the key =  "3b":
    1a:    She
    1b:    sells
    2b:    on
    2c:    the
    3a:    sea
    2a:    shells
    1c:    sea
Note the hashtable after deleting the key = 3b.

The Remove() method of the Hashtable class is used to remove or delete elements from the hashtable one at a time. The Remove() method does not throw an exception if the specified key (argument of the method) is not present in the hashtable. It will simply continue with the program without changing the hashtable.

That’s all about the Remove() method for deleting items from a hashtable collection given a specified key.

Updated on: 14-Dec-2022

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements