C# | Remove the element with the specified key from the Hashtable - GeeksforGeeks

01 Feb, 2019
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Remove(Object) Method is used to remove the element with the specified key from the Hashtable.
Syntax:
public virtual void Remove (object key);

Parameter:
key: It is the key of the element to remove of type System.Object.
Exceptions:
ArgumentNullException: If the key is null.
NotSupportedException: If the Hashtable is read-only or has a fixed size.
Example:
// C# code to remove the element// with the specified key from Hashtableusing System;using System.Collections;  class GFG {      // Driver code    public static void Main()    {          // Creating a Hashtable        Hashtable myTable = new Hashtable();          // Adding elements in Hashtable        myTable.Add("2", "Even & Prime");        myTable.Add("3", "Odd & Prime");        myTable.Add("4", "Even & non-prime");        myTable.Add("9", "Odd & non-prime");          // Print the number of entries in Hashtable        Console.WriteLine("Total number of entries in Hashtable : "                                                   + myTable.Count);          // To remove the elements from Hashtable        // which has key as "3"        myTable.Remove("3");          // Print the number of entries in Hashtable        Console.WriteLine("Total number of entries in Hashtable : "                                                   + myTable.Count);          // To remove the elements from Hashtable        // which has key as "4"        myTable.Remove("4");          // Print the number of entries in Hashtable        Console.WriteLine("Total number of entries in Hashtable : "                                                   + myTable.Count);          // Adding elements in Hashtable        myTable.Add("g", "geeks");        myTable.Add("c", "c++");        myTable.Add("d", "data structures");          // Print the number of entries in Hashtable        Console.WriteLine("Total number of entries in Hashtable : "                                                   + myTable.Count);          // To remove the elements from Hashtable        // which has key as "c"        myTable.Remove("c");          // Print the number of entries in Hashtable        Console.WriteLine("Total number of entries in Hashtable : "                                                   + myTable.Count);    }}
Total number of entries in Hashtable : 4
Total number of entries in Hashtable : 3
Total number of entries in Hashtable : 2
Total number of entries in Hashtable : 5
Total number of entries in Hashtable : 4

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable.remove?view=netframework-4.7.2
CSharp-Collections-Hashtable
CSharp-Collections-Namespace
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Method Overriding
C# | Class and Object
C# | Constructors
C# | String.IndexOf( ) Method | Set - 1
Extension Method in C#
C# | Delegates
Introduction to .NET Framework
C# | Data Types
Difference between Ref and Out keywords in C#
C# | Replace() Method