注意:哈希表Hashtable,Dictionary<string,string>的区别是:Hashtable不指定类型,可以存储任何类型。Dictionary指定类型
Hashtable使用的简单例子:
public void main() { Hashtable ht = new Hashtable(); ht.Add("key1", 12); ht.Add("key2", "test1"); ht.Add("key3", 11.1); ht.Add("key4", "test22"); ht.Add("key5", 12); //修改: ht["key4"] = 5.6; ht["key6"] = 99; //新增一个 //查询1 foreach (DictionaryEntry a in ht) { object key = a.Key; object valur = a.Value; Console.WriteLine($"{key}={valur}"); } //查询2 ICollection keyList = ht.Keys; foreach (object k in keyList) { object key = k; object value = ht[k]; Console.WriteLine($"{key}={value}"); } }