คุณสมบัติ Item ใช้ get/set ค่าที่เกี่ยวข้องกับคีย์ที่ระบุ สามารถเพิ่มสมาชิกใหม่ได้
ถ้าคีย์ไม่มีอยู่ เพิ่มได้ด้วย:
myCollection["myNonexistentKey"] = myValue
ตัวอย่าง
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
Hashtable ht = new Hashtable();
ht.Add("One", "Amit"); ht.Add("Two", "Aman"); ht.Add("Three", "Raman");
ht["Four"] = "David";
ICollection key = ht.Keys;
foreach (string k in key) Console.WriteLine(k + ": " + ht[k]);
Console.ReadKey();
}
}
}
ผลลัพธ์
Three: Raman Four: David One: Amit Two: Aman