获取一个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