The Wayback Machine - https://web.archive.org/web/20240204104856/https://www.geeksforgeeks.org/c-sharp-remove-all-elements-from-the-hashtable/
Open In App
Related Articles

C# | Remove all elements from the Hashtable

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.Clear Method is used to remove all elements from the Hashtable.

Syntax:

myTable.Clear()

Here myTable is the name of the Hashtable.

Exceptions: This method will give NotSupportedException if the Hashtable is read-only.

Example:




// C# code to remove all elements
// from the Hashtable
using 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 all elements from Hashtable
        myTable.Clear();
  
        // 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 all elements from Hashtable
        myTable.Clear();
  
        // Print the number of entries in Hashtable
        Console.WriteLine("Total number of entries in Hashtable : " 
                                                   + myTable.Count);
    }
}


Output:

Total number of entries in Hashtable : 4
Total number of entries in Hashtable : 0
Total number of entries in Hashtable : 3
Total number of entries in Hashtable : 0

Reference:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 01 Feb, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads