ล้าง Hashtable โดยใช้วิธี Clear() ใน C#
ต่อไปนี้คือ Hashtable ของเรา -
Hashtable h = new Hashtable(); h.Add(1, "Amit"); h.Add(2, "Sachin"); h.Add(3, "Rahul");
ใช้วิธีการที่ชัดเจน
h.Clear();
หากตอนนี้คุณพยายามแสดง Hashtable จะไม่มีอะไรแสดงขึ้นเนื่องจาก Hashtable ว่างเปล่า
ตัวอย่าง
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("Keys and Values list:");
foreach (var key in h.Keys ) {
Console.WriteLine("Key = {0}, Value = {1}",key , h[key]);
}
// clear all key value pairs
Console.WriteLine("The Hashtable key and Value Pairs are empty now...");
h.Clear();
foreach (var key in h.Keys ) {
Console.WriteLine("Key = {0}, Value = {1}",key , h[key]);
}
}
} ผลลัพธ์
Keys and Values list: Key = 3, Value = Rahul Key = 2, Value = Sachin Key = 1, Value = Amit The Hashtable key and Value Pairs are empty now...