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

A SortedList in C# is a collection of key-value pairs sorted by keys. To get the list of values from a SortedList object, you can use the GetValueList() method, which returns an IList containing all the values in the same order as their corresponding keys.

Syntax

Following is the syntax for getting values from a SortedList −

SortedList sortedList = new SortedList();
IList valueList = sortedList.GetValueList();

The GetValueList() method returns an IList object containing all values in key-sorted order.

Using GetValueList() with Integer Values

This example demonstrates how to extract integer values from a SortedList −

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("A", 1);
      list.Add("B", 2);
      list.Add("C", 3);
      list.Add("D", 4);
      list.Add("E", 5);
      list.Add("F", 6);
      list.Add("G", 7);
      list.Add("H", 8);
      
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      Console.WriteLine("\nList of values...SortedList");
      IList col = list.GetValueList();
      foreach(int res in col)
         Console.WriteLine(res);
   }
}

The output of the above code is −

SortedList elements...
A 1
B 2
C 3
D 4
E 5
F 6
G 7
H 8

List of values...SortedList
1
2
3
4
5
6
7
8

Using GetValueList() with String Values

This example shows how to extract string values representing department names −

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("One", "IT");
      list.Add("Two", "Operations");
      list.Add("Three", "Marketing");
      list.Add("Four", "Purchase");
      list.Add("Five", "Sales");
      list.Add("Six", "Finance");
      
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      Console.WriteLine("\nList of values...SortedList");
      IList col = list.GetValueList();
      foreach(string res in col)
         Console.WriteLine(res);
   }
}

The output of the above code is −

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

List of values...SortedList
Sales
Purchase
IT
Finance
Marketing
Operations

How It Works

The GetValueList() method creates a read-only IList wrapper around the values collection. The values maintain the same order as their corresponding keys in the sorted list. Since SortedList automatically sorts entries by key, the values are returned in the order of their sorted keys, not in the order they were added.

Key Points

  • The returned IList is read-only − you cannot modify it directly.

  • Values are returned in the same order as their sorted keys.

  • The method works with any value type stored in the SortedList.

  • Changes to the original SortedList are reflected in the returned list.

Conclusion

The GetValueList() method provides an efficient way to extract all values from a SortedList while maintaining the key-sorted order. This is useful when you need to process only the values without their corresponding keys.

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

161 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements