优快云广告是越来越多了,所有博客笔记不再更新,新网址 DotNet笔记
msdn中Hashtable类:点击打开hashtable类链接
DictionaryEntry 结构:点击打开DictionaryEntry链接
C# 语言中的 foreach 语句需要集合中每个元素的类型。由于 IDictionary 的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 DictionaryEntry 类型。
Hashtable ht = new Hashtable();
ht.Add("job", "a");
ht.Add("jobmon", "20");
//单个取值,方法比较特别
string a = ht["jobmon"].ToString();
//Console.WriteLine(a);
//第一种方法遍历
foreach(DictionaryEntry de in ht)
{
Console.WriteLine(de.Key);
Console.WriteLine(de.Value);
}
Console.WriteLine("-------------------------");
//第二种方法遍历
IDictionaryEnumerator enumerator = ht.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumerator.Key);
Console.WriteLine(enumerator.Value);
}
Console.WriteLine("++++++++++++++++++++++++++"