C# | Getting the list of keys of a SortedList object
SortedList.GetKeyList Method is used to get the list of keys in a SortedList object.
Syntax:
public virtual System.Collections.IList GetKeyList ();
Return Value: It returns an IList object containing the keys in the SortedList object.
Below programs illustrate the use of above-discussed method:
Example 1:
using System;
using System.Collections;
class Geeks {
public static void Main(String[] args)
{
SortedList mylist = new SortedList();
mylist.Add( "1" , "C++" );
mylist.Add( "2" , "Java" );
mylist.Add( "3" , "DSA" );
mylist.Add( "4" , "Python" );
mylist.Add( "5" , "C#" );
IList klist = mylist.GetKeyList();
Console.WriteLine( "Key List" );
for ( int i = 0; i < mylist.Count; i++)
Console.WriteLine(klist[i]);
}
}
|
Output:
Key List
1
2
3
4
5
Example 2:
using System;
using System.Collections;
class Geeks {
public static void Main(String[] args)
{
SortedList mylist = new SortedList();
mylist.Add( "First" , "Ram" );
mylist.Add( "Second" , "Shyam" );
mylist.Add( "Third" , "Mohit" );
mylist.Add( "Fourth" , "Rohit" );
mylist.Add( "Fifth" , "Manish" );
IList klist = mylist.GetKeyList();
Console.WriteLine( "Key List" );
for ( int i = 0; i < mylist.Count; i++)
Console.WriteLine(klist[i]);
}
}
|
Output:
Key List
Fifth
First
Fourth
Second
Third
Note:
- The returned IList object is a read-only view of the keys of the SortedList object. Modifications made to the underlying SortedList are immediately reflected in the IList.
- The elements of the returned IList are sorted in the same order as the keys of the SortedList.
- This method is similar to the Keys property but returns an IList object instead of an ICollection object.
- This method is an O(1) operation.
Reference:
Last Updated :
01 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...