Home > Backend Development > C#.Net Tutorial > How to remove items from hash table collection in C#?

How to remove items from hash table collection in C#?

王林
Release: 2023-09-09 21:01:06
forward
678 people have browsed it

如何在 C# 中从哈希表集合中删除项目?

Hashtable in C# is a collection of key-value pairs organized based on key hash codes. Items in a hashtable can be accessed using keys. C#'s Hashtable class is a class that implements hashtable.

Using this class, we can create a new hash table object with the help of the provided constructor. The Hashtable class also provides various methods using which we can perform various operations on the hash table. These operations include adding an item, checking if a specified key exists, counting the number of items, deleting items from a hash table, etc.

In this article, we will discuss removing an item from a hash table collection given a specified key.

How to delete items from Hashtable collection?

The Hashtable class provides a method named "Remove" for removing an item from the hashtable collection. Given a key, the Remove method will remove the item with the specified key from the hashtable.

The prototype of the Remove method is given below.

grammar

public virtual void Remove (object key);
Copy after login

parameter

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

accomplish

The

Remove(Object) method is part of the IDictionary interface.

abnormal

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

  • NotSupportedException − Thrown if the hash table has a fixed size or is read-only.

If the specified key does not exist in the hash table, the Remove() method will not throw any exception. If the key does not exist, the hash table will remain unchanged and the program will execute successfully.

Programming example of Remove() method

Example 1

The following program demonstrates how to use the Remove() method to remove items from a hash table 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]);
      }
   }
}
Copy after login

In this program, first we create a hash table, where the keys are numbers and the values ​​are the corresponding numeric names. The hash table is then displayed on the screen. Next, the user is prompted for the key of the element to be removed from the hash table. Once the key is entered, the Remove() method is called, passing the key as a parameter. Then display the contents of the hash table again. If the key exists in the hash table, the Remove() method will remove the element, otherwise, the hash table 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
Copy after login

The above output shows the difference in the contents of the hash table before and after deletion.

Now let's see how the output changes when the user enters a key that does not exist in the hash table. In this case, as already mentioned, the hash table remains unchanged and no exception is thrown. The following 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
Copy after login

Here, the user entered key = 3, but it does not exist in the hash table. In this case, since the Remove() method does not remove any elements, the hash table remains unchanged.

Example 2

Now let us consider another example of hash table deletion. The corresponding procedure is shown 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();
   }
}
Copy after login

In this program we have a hash table containing the tongue twister "She sells seashells at the beach". We number the keys 1a, 1b, 1c, 2a, 2b, etc. First, we display the entire hash table. Then we use the Remove() method and delete the element with key = 3b. The newly updated hash table 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.
Copy after login

The Remove() method of the Hashtable class is used to delete or delete elements in the hash table one by one. The Remove() method does not throw an exception if the specified key (parameter of the method) does not exist in the hash table. It will continue executing the program without changing the hash table.

That’s all about the Remove() method, which is used to remove items from a hash table collection based on a specified key.

The above is the detailed content of How to remove items from hash table collection in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template