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

In C#, the SortedList class provides the GetByIndex() method to retrieve the value at a specific index position. Unlike dictionary access by key, this method allows you to access values by their ordered position within the sorted collection.

Syntax

Following is the syntax for using GetByIndex() method −

public virtual object GetByIndex(int index);

Parameters

  • index: The zero-based index of the value to retrieve from the SortedList.

Return Value

The method returns an object representing the value at the specified index position. If the index is out of range, an ArgumentOutOfRangeException is thrown.

SortedList Index Access Key: "A" "B" "C" "D" "E" "F" "G" Value: Jacob Sam Tom John Tim Mark Gary Index: 0 1 2 3 4 5 6 GetByIndex(2) returns "Tom"

Using GetByIndex() with String Keys

Example

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("A", "Jacob");
      list.Add("B", "Sam");
      list.Add("C", "Tom");
      list.Add("D", "John");
      list.Add("E", "Tim");
      list.Add("F", "Mark");
      list.Add("G", "Gary");
      Console.WriteLine("Value at index 2 = " + list.GetByIndex(2));
      Console.WriteLine("Value at index 5 = " + list.GetByIndex(5));
      Console.WriteLine("Value at index 6 = " + list.GetByIndex(6));
   }
}

The output of the above code is −

Value at index 2 = Tom
Value at index 5 = Mark
Value at index 6 = Gary

Using GetByIndex() with Integer Keys

Example

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add(1, "One");
      list.Add(2, "Two");
      list.Add(3, "Three");
      list.Add(4, "Four");
      list.Add(5, "Five");
      Console.WriteLine("Value at index 0 = " + list.GetByIndex(0));
      Console.WriteLine("Value at index 2 = " + list.GetByIndex(2));
      Console.WriteLine("Value at index 4 = " + list.GetByIndex(4));
   }
}

The output of the above code is −

Value at index 0 = One
Value at index 2 = Three
Value at index 4 = Five

Key Rules

  • The GetByIndex() method uses zero-based indexing, where the first element is at index 0.

  • The SortedList maintains elements in sorted order by key, not by insertion order.

  • The index position corresponds to the sorted position, not the original insertion position.

  • An ArgumentOutOfRangeException is thrown if the index is negative or greater than or equal to the Count.

Conclusion

The GetByIndex() method provides efficient access to values in a SortedList by their sorted position. This is particularly useful when you need to iterate through values in sorted order or access elements by their ordinal position rather than their key.

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

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements