Hashtable.Keys Property is used to get an ICollection containing the keys in the Hashtable.
Syntax:
public virtual System.Collections.ICollection Keys { get; }
Return Value: This property returns an ICollection containing the keys in the Hashtable.
Note:
- The order of keys in the ICollection is unspecified.
- Retrieving the value of this property is an O(1) operation.
Below programs illustrate the use of above-discussed property:
Example 1:
// C# code to get an ICollection containing // the keys in the Hashtable. using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add("4", "Even"); myTable.Add("9", "Odd"); myTable.Add("5", "Odd and Prime"); myTable.Add("2", "Even and Prime"); // Get a collection of the keys. ICollection c = myTable.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + myTable[str]); } } |
chevron_right
filter_none
Output:
5: Odd and Prime 9: Odd 2: Even and Prime 4: Even
Example 2:
// C# code to get an ICollection containing // the keys in the Hashtable. using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add("India", "Country"); myTable.Add("Chandigarh", "City"); myTable.Add("Mars", "Planet"); myTable.Add("China", "Country"); // Get a collection of the keys. ICollection c = myTable.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + myTable[str]); } } |
chevron_right
filter_none
Output:
Chandigarh: City India: Country China: Country Mars: Planet
Reference:
Recommended Posts:
- C# | Gets an ICollection containing the values in the Hashtable
- C# | Get an ICollection containing the keys in ListDictionary
- C# | Get an ICollection containing the keys in HybridDictionary
- C# | Get an ICollection containing keys in OrderedDictionary
- C# | Check if a Hashtable is equal to another Hashtable
- C# | Get an ICollection containing the values in HybridDictionary
- C# | Get an ICollection containing values in OrderedDictionary
- C# | Get an ICollection containing the values in ListDictionary
- C# | Get or Set the value associated with specified key in Hashtable
- C# | Hashtable Class
- C# Hashtable with Examples
- Difference between Hashtable and Dictionary in C#
- C# | Remove all elements from the Hashtable
- C# | Adding an element into the Hashtable
- C# | Check if the Hashtable contains a specific Value
- C# | Check if the Hashtable contains a specific Key
- C# | How to get hash code for the specified key of a Hashtable
- C# | Get an enumerator that iterates through the Hashtable
- C# | Check whether a Hashtable contains a specific key or not
- C# | Remove the element with the specified key from the Hashtable
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

