Hashtable.Clear() xóa tất cả cặp key-value khỏi Hashtable, để nó trống (Count = 0).
Ví dụ minh họa
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
Hashtable h = new Hashtable();
h.Add(1, "Amit");
h.Add(2, "Sachin");
h.Add(3, "Rahul");
Console.WriteLine("Danh sách Key và Value:");
foreach (var key in h.Keys)
Console.WriteLine("Key = {0}, Value = {1}", key, h[key]);
Console.WriteLine("Hashtable rỗng sau Clear()...");
h.Clear();
foreach (var key in h.Keys)
Console.WriteLine("Key = {0}, Value = {1}", key, h[key]);
}
}
Kết quả đầu ra
Danh sách Key và Value: Key = 3, Value = Rahul Key = 2, Value = Sachin Key = 1, Value = Amit Hashtable rỗng sau Clear()...
Lưu ý: Hashtable là kiểu cũ (non-generic), khuyến nghị dùng Dictionary<TKey, TValue> cho type safety và hiệu năng tốt hơn.