Dictionary类与 Hashtable 类的功能相同
HashTable ht=new HashTable();//实现 IDictionary接口
ht.Add(1,"A");
ht.Add(2,"B");
ht.Add(3,"c");
foreach(DictionaryEntry de in ht)//HashTable返回的是DictionaryEntry类型
{
de.Key;
de.Value;
}
Dictionary<int,string> myDictionary=new Dictionary<int,string>();//实现IDictionary接口,IDictionary<T key,T value>类
myDictionary.Add(1,"a");
myDictionary.Add(2,"b");
myDictionary.Add(3,"c");
foreach(int i in myDictionary.Keys)
{
Console.WriteLine("Key="+i+"Value="+myDictionary);
}
Or
foreach(KeyValuePair<string, double> temp in myDictionary)//返回的是KeyValuePair<string, double>泛型数组
{
temp.Key;
temp.Value;
}
2者都是通过循环来便利 来通过键得到值的。
HashTable 和Dictionary的区别,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/YZDK/p/3836812.html