Getting the Values in a SortedList object in C#

The SortedList class in C# is a collection that stores key-value pairs sorted by the keys. To retrieve all values from a SortedList object, you can use the Values property, which returns an ICollection containing all the values in sorted order of their corresponding keys.

Syntax

Following is the syntax for accessing values in a SortedList −

SortedList sortedList = new SortedList();
ICollection values = sortedList.Values;

The Values property returns an ICollection that can be iterated using a foreach loop −

foreach (var value in sortedList.Values) {
   Console.WriteLine(value);
}

Using SortedList with Integer Keys

Here's an example demonstrating how to retrieve values from a SortedList with integer keys −

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");
      
      ICollection col1 = list.Values;
      Console.WriteLine("Values...");
      foreach(string s in col1)
         Console.WriteLine(s);
      
      ICollection col2 = list.Keys;
      Console.WriteLine("Keys...");
      foreach(int s in col2)
         Console.WriteLine(s);
   }
}

The output of the above code is −

Values...
One
Two
Three
Four
Five
Keys...
1
2
3
4
5

Using SortedList with String Keys

The following example shows retrieving values from a SortedList with string keys −

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");
      list.Add("H", "Nathan");
      list.Add("I", "Shaun");
      list.Add("J", "David");
      
      ICollection col1 = list.Values;
      Console.WriteLine("Values...");
      foreach(string s in col1)
         Console.WriteLine(s);
      
      ICollection col2 = list.Keys;
      Console.WriteLine("\nKeys...");
      foreach(string s in col2)
         Console.WriteLine(s);
   }
}

The output of the above code is −

Values...
Jacob
Sam
Tom
John
Tim
Mark
Gary
Nathan
Shaun
David

Keys...
A
B
C
D
E
F
G
H
I
J

Accessing Individual Values by Index

You can also access individual values using the GetByIndex() method or by using the indexer with keys −

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("C", "Apple");
      list.Add("A", "Orange");
      list.Add("B", "Banana");
      
      Console.WriteLine("Value at index 0: " + list.GetByIndex(0));
      Console.WriteLine("Value for key 'B': " + list["B"]);
      Console.WriteLine("Value for key 'C': " + list["C"]);
      
      Console.WriteLine("\nAll values:");
      for (int i = 0; i < list.Count; i++) {
         Console.WriteLine("Index " + i + ": " + list.GetByIndex(i));
      }
   }
}

The output of the above code is −

Value at index 0: Orange
Value for key 'B': Banana
Value for key 'C': Apple

All values:
Index 0: Orange
Index 1: Banana
Index 2: Apple

Conclusion

The Values property of SortedList provides access to all values in the collection, returned in the sorted order of their keys. You can iterate through these values using foreach loops or access individual values using GetByIndex() method or key-based indexing.

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

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements