Hashtable ht=new Hashtable (); //声明
//利用foreach循环遍历Hashtable
foreach (var item in ht.Key ){
Console.WriteLine(“键是{0}=====值是{1}", item, ht[item]);
}
①Add( object key, object value ) //键必须唯一,值可以重复
②ContainsKey( object key ) //判断 Hashtable 是否包含指定的键。
③ContainsValue( object value ); //判断 Hashtable 是否包含指定的值。
④Remove( object key ); //从 Hashtable 中移除带有指定的键的元素。
⑤Clear(); //从 Hashtable 中移除所有的元素
Hashtable的泛型版本Dictionary字典集合
Dictionary<string,int> dict=new Dictionary<string,int>;
dict.Add(“zhang”,100);
dict.Add(“zhengyao”,80);
//遍历键
foreach(string item in dict.Keys){
Console.WriteLine(item);
}
//遍历值
foreach(string item in dict.Values){
Console.WriteLine(item);
}
//遍历键值对 必须写KeyValuePair
foreach (KeyValuePair<string,int> item in dict){
Console.writeLine("{0}--{1}", item .Key, item .Value);
}