What is the Keys property of Hashtable class in C#?


Gets an ICollection containing the keys in the Hashtable. It displays all the keys in the collection. In the below code, to get all the keys we have used a loop to loop through the collection.

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

The above displays all the keys as shown in the following code −

Example

 Live Demo

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);
      }
   }
}

Output

5
4
3
2
1

Updated on: 23-Jun-2020

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements