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

The Keys property of the Hashtable class in C# returns an ICollection containing all the keys in the hashtable. This property is useful for iterating through all keys or performing operations on the key collection without accessing the values.

Syntax

Following is the syntax for accessing the Keys property −

public virtual ICollection Keys { get; }

Return Value

The Keys property returns an ICollection object that contains all the keys in the hashtable. The order of keys is not guaranteed as hashtables do not maintain insertion order.

Using Keys Property to Display All Keys

You can iterate through all keys using a foreach loop −

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, "Netherlands");
      
      Console.WriteLine("Keys in the hashtable:");
      foreach (int k in h.Keys) {
         Console.WriteLine(k);
      }
   }
}

The output of the above code is −

Keys in the hashtable:
5
4
3
2
1

Using Keys Property with Different Data Types

The Keys property works with any data type used as keys in the hashtable −

using System;
using System.Collections;

class Program {
   static void Main() {
      Hashtable h = new Hashtable();
      h.Add("name", "John");
      h.Add("age", 25);
      h.Add("city", "New York");
      h.Add("country", "USA");
      
      Console.WriteLine("String keys in the hashtable:");
      foreach (string key in h.Keys) {
         Console.WriteLine($"Key: {key}, Value: {h[key]}");
      }
   }
}

The output of the above code is −

String keys in the hashtable:
Key: country, Value: USA
Key: city, Value: New York
Key: age, Value: 25
Key: name, Value: John

Converting Keys to Array

You can convert the Keys collection to an array for further processing −

using System;
using System.Collections;

class Program {
   static void Main() {
      Hashtable h = new Hashtable();
      h.Add("A", 1);
      h.Add("B", 2);
      h.Add("C", 3);
      
      // Convert keys to array
      string[] keysArray = new string[h.Keys.Count];
      h.Keys.CopyTo(keysArray, 0);
      
      Console.WriteLine("Keys as array:");
      for (int i = 0; i < keysArray.Length; i++) {
         Console.WriteLine($"Index {i}: {keysArray[i]}");
      }
      
      Console.WriteLine($"\nTotal number of keys: {h.Keys.Count}");
   }
}

The output of the above code is −

Keys as array:
Index 0: C
Index 1: B
Index 2: A

Total number of keys: 3

Key Features

  • The Keys property is read-only − you cannot modify the collection directly.

  • The order of keys is not guaranteed and may vary between different executions.

  • The Keys collection is a live view − changes to the hashtable are reflected in the Keys collection.

  • You can use the Count property to get the number of keys.

Conclusion

The Keys property of the Hashtable class provides access to all keys in the collection as an ICollection. It is particularly useful for iterating through keys, converting them to arrays, or checking the total number of keys in the hashtable.

Updated on: 2026-03-17T07:04:35+05:30

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements