Hashtable ht = new Hashtable();
ht.Add(1, 11); // int 鍵
ht.Add("1", "我的鍵是字串1"); // string 鍵
ht.Add(5, "鍵是5,優先顯示");
ht[3] = "用索引器賦值";
Console.WriteLine("元素數:" + ht.Count);
// 遍歷 DictionaryEntry
foreach (DictionaryEntry de in ht)
Console.WriteLine("鍵:{0} 值:{1}", de.Key, de.Value);
輸出順序依哈希碼,非插入順序。鍵 1 (int) 與 "1" (string) 視為不同鍵。
ht[3] = "修改後的值";
foreach (object k in ht.Keys)
Console.WriteLine("鍵:{0} 值:{1}", k, ht[k]);