C# |从哈希表中移除所有元素
哈希表类表示基于键的哈希代码组织的键和值对的集合。该键用于访问集合中的项。哈希表。清除方法用于从哈希表中移除所有元素。
语法:
myTable.Clear()
这里 myTable 是哈希表的名称。
异常:如果哈希表是只读的,这个方法将给出 NotSupportedException 。
示例:
// 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);
}
}
输出:
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
参考:
版权属于:月萌API www.moonapi.com,转载请注明出处