MSDN Documentation

SortedList Class

Represents a collection of key/value pairs that are individually sorted on the keys and that are accessible by both the key and the index.

Namespace:

System.Collections

Assembly:

mscorlib.dll

Syntax

public class SortedList : IDictionary, ICollection, IEnumerable, ICloneable

Remarks

The SortedList class represents a collection of key/value pairs that are kept sorted by keys. The keys are unique and cannot be null. The values can be null.

The SortedList is implemented using two arrays, one for the keys and one for the values. The keys are sorted, so searching for a key is efficient. However, adding or removing elements can be slow because it may require shifting all the elements in the arrays.

If you need a collection that is sorted by keys but allows duplicate keys, consider using SortedDictionary<TKey, TValue> (available in .NET Framework 4 and later). If you need a collection that is sorted by values, you will need to implement your own custom sorting logic.

Constructors

Name Description
SortedList() Initializes a new instance of the SortedList class that is empty, has the default initial capacity, and is sorted using a case-sensitive comparison.
SortedList(int capacity) Initializes a new instance of the SortedList class that is empty, has the specified initial capacity, and is sorted using a case-sensitive comparison.
SortedList(IComparer comparer) Initializes a new instance of the SortedList class that is empty, has the default initial capacity, and is sorted using the specified IComparer.
SortedList(IComparer comparer, int capacity) Initializes a new instance of the SortedList class that is empty, has the specified initial capacity, and is sorted using the specified IComparer.

Properties

Name Description
Count Gets the number of key/value pairs contained in the SortedList.
Keys Gets an ICollection containing the keys in the SortedList.
Values Gets an ICollection containing the values in the SortedList.
IsFixedSize Gets a value indicating whether the SortedList has a fixed size.
IsReadOnly Gets a value indicating whether the SortedList is read-only.
Capacity Gets or sets the initial capacity of the SortedList.

Methods

Name Description
Add(object key, object value) Adds an element with the specified key and value to the SortedList.
Clear() Removes all elements from the SortedList.
Clone() Creates a shallow copy of the SortedList.
Contains(object key) Determines whether the SortedList contains the specified key.
ContainsKey(object key) Determines whether the SortedList contains the specified key.
ContainsValue(object value) Determines whether the SortedList contains the specified value.
GetByIndex(int index) Gets the value at the specified index.
GetKey(int index) Gets the key at the specified index.
IndexOfKey(object key) Gets the zero-based index of the specified key in the SortedList.
IndexOfValue(object value) Gets the zero-based index of the first occurrence of the specified value in the SortedList.
Remove(object key) Removes the element with the specified key from the SortedList.
RemoveAt(int index) Removes the element at the specified index from the SortedList.
SetByIndex(int index, object value) Sets the value at the specified index.

Example

The following example demonstrates how to use the SortedList class to store and retrieve key/value pairs.

using System;
using System.Collections;

public class SortedListExample
{
    public static void Main(string[] args)
    {
        // Create a SortedList
        SortedList sortedList = new SortedList();

        // Add elements to the SortedList
        sortedList.Add("Apple", 1);
        sortedList.Add("Banana", 2);
        sortedList.Add("Cherry", 3);
        sortedList.Add("Date", 4);

        Console.WriteLine("Initial SortedList:");
        PrintSortedList(sortedList);

        // Access elements by key
        Console.WriteLine($"Value for 'Banana': {sortedList["Banana"]}");

        // Access elements by index
        Console.WriteLine($"Key at index 1: {sortedList.GetKey(1)}, Value at index 1: {sortedList.GetByIndex(1)}");

        // Check if a key exists
        Console.WriteLine($"Contains key 'Cherry': {sortedList.ContainsKey("Cherry")}");

        // Remove an element by key
        sortedList.Remove("Banana");
        Console.WriteLine("\nSortedList after removing 'Banana':");
        PrintSortedList(sortedList);

        // Remove an element by index
        sortedList.RemoveAt(0); // Removes 'Apple'
        Console.WriteLine("\nSortedList after removing element at index 0:");
        PrintSortedList(sortedList);

        // Clear the SortedList
        sortedList.Clear();
        Console.WriteLine($"\nSortedList after clearing. Count: {sortedList.Count}");
    }

    public static void PrintSortedList(SortedList list)
    {
        foreach (DictionaryEntry entry in list)
        {
            Console.WriteLine($"  Key: {entry.Key}, Value: {entry.Value}");
        }
    }
}