SortedList.Keys Property is used to get the keys in a SortedList object.
Syntax:
public virtual System.Collections.ICollection Keys { get; }
Property Value: An ICollection object containing the keys in the SortedList object.
Below programs illustrate the use of above-discussed property:
Example 1:
using System;
using System.Collections;
class GFG {
public static void Main()
{
SortedList mylist = new SortedList();
mylist.Add( "4" , "Even" );
mylist.Add( "9" , "Odd" );
mylist.Add( "5" , "Odd and Prime" );
mylist.Add( "2" , "Even and Prime" );
ICollection c = mylist.Keys;
foreach ( string str in c)
Console.WriteLine(str + ": " + mylist[str]);
}
}
|
Output:
2: Even and Prime
4: Even
5: Odd and Prime
9: Odd
Example 2:
using System;
using System.Collections;
class GFG {
public static void Main()
{
SortedList mylist = new SortedList();
mylist.Add( "India" , "Country" );
mylist.Add( "Chandigarh" , "City" );
mylist.Add( "Mars" , "Planet" );
mylist.Add( "China" , "Country" );
ICollection c = mylist.Keys;
foreach ( string str in c)
Console.WriteLine(str + ": " + mylist[str]);
}
}
|
Output:
Chandigarh: City
China: Country
India: Country
Mars: Planet
Note:
- The ICollection object is a read-only view of the keys of the SortedList object. Modifications made to the underlying SortedList are immediately reflected in the ICollection.
- The elements of the ICollection are sorted in the same order as the keys of the SortedList.
- This property is similar to the GetKeyList method but returns an ICollection object instead of an IList object.
- This method is an O(1) operation.
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Feb, 2019
Like Article
Save Article