C# 中 Hashtable 类的 Keys 属性表示什么?


获取一个包含 Hashtable 中键的 ICollection。它显示了集合中的所有键。在下面的代码中,要获取所有键,我们使用了一个循环来循环遍历集合。

foreach (int k in h.Keys) {
   Console.WriteLine(k);
}

上述代码显示了所有键,如下面的代码所示 −

示例

 现场演示

using System;
using System.Collections;
class Program {
   static void Main() {
      Hashtable h = new Hashtable();
      h.Add(1, "India");
      h.Add(2, "US");
      h.Add(3, "UK");
      h.Add(4, "Australia");
      h.Add(5, "Netherland");
      foreach (int k in h.Keys) {
         Console.WriteLine(k);
      }
   }
}

输出

5
4
3
2
1

更新于:23-Jun-2020

88 次浏览

启动你的 职业生涯

完成课程认证

开始行动
广告
© . All rights reserved.