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.