Getting the list of keys of a SortedList object in C#

The SortedList class in C# provides the GetKeyList() method to retrieve all keys as an IList collection. This method returns keys in their sorted order, making it useful for accessing or iterating through keys separately from their values.

Syntax

Following is the syntax for getting the list of keys from a SortedList −

IList keyList = sortedList.GetKeyList();

Return Value

The GetKeyList() method returns an IList object containing all the keys from the SortedList in sorted order. The returned list is read-only and reflects the current state of the SortedList.

Using GetKeyList() Method

Example

using System;
using System.Collections;

public class Demo {
    public static void Main(String[] args) {
        SortedList list1 = new SortedList();
        list1.Add("One", 1);
        list1.Add("Two", 2);
        list1.Add("Three", 3);
        list1.Add("Four", 4);
        list1.Add("Five", 5);
        list1.Add("Six", 6);
        list1.Add("Seven", 7);
        list1.Add("Eight", 8);
        list1.Add("Nine", 9);
        list1.Add("Ten", 10);
        
        Console.WriteLine("SortedList1 elements...");
        foreach(DictionaryEntry d in list1) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("\nList of keys...SortedList1");
        IList list = list1.GetKeyList();
        foreach(string res in list)
            Console.WriteLine(res);
            
        SortedList list2 = new SortedList();
        list2.Add("A", "Accessories");
        list2.Add("B", "Books");
        list2.Add("C", "Smart Wearable Tech");
        list2.Add("D", "Home Appliances");
        
        Console.WriteLine("\nSortedList2 elements...");
        foreach(DictionaryEntry d in list2) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("\nList of keys...SortedList2");
        list = list2.GetKeyList();
        foreach(string res in list)
            Console.WriteLine(res);
    }
}

The output of the above code is −

SortedList1 elements...
Eight 8
Five 5
Four 4
Nine 9
One 1
Seven 7
Six 6
Ten 10
Three 3
Two 2

List of keys...SortedList1
Eight
Five
Four
Nine
One
Seven
Six
Ten
Three
Two

SortedList2 elements...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances

List of keys...SortedList2
A
B
C
D

Working with Specific Key Types

Example

using System;
using System.Collections;

public class Demo {
    public static void Main(String[] args) {
        SortedList list = new SortedList(); 
        list.Add("One", 1);
        list.Add("Two", 2);
        list.Add("Three", 3);
        list.Add("Four", 4);
        list.Add("Five", 5);
        
        Console.WriteLine("SortedList elements...");
        foreach(DictionaryEntry d in list) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("\nList of keys...SortedList");
        IList col = list.GetKeyList();
        foreach(string res in col)
            Console.WriteLine(res);
            
        Console.WriteLine("\nKey count: " + col.Count);
        Console.WriteLine("First key: " + col[0]);
        Console.WriteLine("Last key: " + col[col.Count - 1]);
    }
}

The output of the above code is −

SortedList elements...
Five 5
Four 4
One 1
Three 3
Two 2

List of keys...SortedList
Five
Four
One
Three
Two

Key count: 5
First key: Five
Last key: Two

Key Properties of GetKeyList()

Property Description
Read-only The returned IList is read-only and cannot be modified
Sorted Order Keys are returned in the same sorted order as in SortedList
Live View Changes to the SortedList are reflected in the key list
Index Access Keys can be accessed by index using keyList[index]

Conclusion

The GetKeyList() method provides an efficient way to retrieve all keys from a SortedList as a read-only IList collection. The keys maintain their sorted order and provide indexed access, making it useful for scenarios where you need to work with keys independently of their values.

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

801 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements