Getting the key at the specified index of a SortedList object in C#

The SortedList class in C# maintains its elements in sorted order by key. To get the key at a specified index, use the GetKey() method. Since SortedList automatically sorts elements alphabetically by key, the index positions correspond to the sorted order, not the insertion order.

Syntax

Following is the syntax for getting a key at the specified index −

public virtual object GetKey(int index)

Parameters

  • index − The zero-based index of the key to get.

Return Value

Returns the key at the specified index of the SortedList object.

Using GetKey() Method

The GetKey() method retrieves the key at a specific index position. Remember that SortedList sorts keys alphabetically, so the index corresponds to the sorted position −

SortedList Key Access by Index Index: Key: Value: 0 Eight 8 1 Five 5 2 Four 4 3 Nine 9 4 One 1 ... GetKey(2) = "Four"

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("Index at key Five = " + list1.IndexOfKey("Five"));
      Console.WriteLine("Key at index 2 = " + list1.GetKey(2));

      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("Index at key B = " + list2.IndexOfKey("B"));
   }
}

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
Index at key Five = 1
Key at index 2 = Four

SortedList2 elements...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances
Index at key B = 1

Getting Keys from Different Data Types

Example

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("One", "Finance");
      list.Add("Two", "Marketing");
      list.Add("Three", "Sales");
      list.Add("Four", "Purchase");
      list.Add("Five", "Operations");
      list.Add("Six", "IT");

      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }

      Console.WriteLine("\nIndex at key One = " + list.IndexOfKey("One"));
      Console.WriteLine("Key at index 5 = " + list.GetKey(5));

      // Display all keys using GetKey method
      Console.WriteLine("\nAll keys by index:");
      for(int i = 0; i < list.Count; i++) {
         Console.WriteLine("Index " + i + ": " + list.GetKey(i));
      }
   }
}

The output of the above code is −

SortedList elements...
Five Operations
Four Purchase
One Finance
Six IT
Three Sales
Two Marketing

Index at key One = 2
Key at index 5 = Two

All keys by index:
Index 0: Five
Index 1: Four
Index 2: One
Index 3: Six
Index 4: Three
Index 5: Two

Key Rules

  • SortedList automatically sorts elements by key in ascending alphabetical order.

  • Index positions are zero-based and correspond to the sorted order.

  • Use IndexOfKey() to find the index of a specific key.

  • Use GetKey() to retrieve the key at a specific index position.

Conclusion

The GetKey() method provides efficient access to keys by their index position in a SortedList. Since SortedList maintains elements in sorted order, the index corresponds to the alphabetically sorted position of keys, making it useful for ordered data retrieval operations.

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

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements