C# Hashtable的遍历

来源:互联网 发布:电脑钢琴软件带曲谱 编辑:程序博客网 时间:2024/05/21 00:47

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("++++++++++++++++++++++++++"

0 0
原创粉丝点击