Last Updated : 11 Jul, 2025
The Hashtable class represents a collection of
key-and-value pairsthat are organized based on the hash code of the key. The key is used to access the items in the collection.
Hashtable.Clear Methodis used to remove all elements from the Hashtable.
Syntax:myTable.Clear()
Here
myTableis the name of the Hashtable.
Exceptions:This method will give
NotSupportedExceptionif the Hashtable is read-only.
Example: CSHARP
// 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 : 0Reference:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4