C# | Gets an ICollection containing the keys in the Hashtable
Last Updated :
01 Feb, 2019
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:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Hashtable myTable = new Hashtable();
myTable.Add( "4" , "Even" );
myTable.Add( "9" , "Odd" );
myTable.Add( "5" , "Odd and Prime" );
myTable.Add( "2" , "Even and Prime" );
ICollection c = myTable.Keys;
foreach ( string str in c)
Console.WriteLine(str + ": " + myTable[str]);
}
}
|
Output:
5: Odd and Prime
9: Odd
2: Even and Prime
4: Even
Example 2:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Hashtable myTable = new Hashtable();
myTable.Add( "India" , "Country" );
myTable.Add( "Chandigarh" , "City" );
myTable.Add( "Mars" , "Planet" );
myTable.Add( "China" , "Country" );
ICollection c = myTable.Keys;
foreach ( string str in c)
Console.WriteLine(str + ": " + myTable[str]);
}
}
|
Output:
Chandigarh: City
India: Country
China: Country
Mars: Planet
Reference:
Share your thoughts in the comments
Please Login to comment...