class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("a", "a1");
ht.Add("b", "b1");
ht.Add("c", "c1");
// 遍历HashTable
ArrayList akey = new ArrayList(ht.Keys);
foreach (string key in akey) {
Console.WriteLine("{0},{1}", key, ht[key]);
}
foreach (DictionaryEntry de in ht) {
Console.WriteLine("{0},{1}", de.Key, de.Value);
}
IDictionaryEnumerator en = ht.GetEnumerator();
while (en.MoveNext()) {
string strKey = en.Key.ToString();
Console.WriteLine(strKey);
string strValue = en.Value.ToString();
Console.WriteLine(strValue);
}
Console.ReadKey();
}
}
Dictionary<object, string> arr = new Dictionary<object, string>();
arr.Add(10, "a");
arr.Add(20, "120");
//遍历键值第一种方式
foreach (object obj in arr.Keys) {
Console.WriteLine(arr[obj]);
}
foreach (string str in arr.Values) {
Console.WriteLine(str);
}
//DictionaryEntry
//遍历键值第二种方式
foreach (KeyValuePair<object,string> obj in arr) {
Console.WriteLine(obj.Key+"--"+obj.Value);
}
foreach (var obj in arr)//var 隐式转换所需要的类型
{
String str = obj.Value;
Console.WriteLine(obj.Key + "--" + str);
}