SortedList Class

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

public sealed class SortedList : ICollection, ICloneable, IDictionary, IEnumerable, IHashCodeProvider
Members

Summary

The SortedList class in the System.Collections namespace provides a data structure that stores elements as key/value pairs, similar to a dictionary or hash table. However, SortedList maintains its elements in ascending order based on their keys. This sorting allows for efficient retrieval of elements by index and ensures that iterating through the collection yields elements in a predictable order.

SortedList implements the IDictionary interface, meaning each element has a unique key and a corresponding value. Keys and values can be of any type (object). However, for comparisons to work correctly, the keys must implement the IComparable interface.

Properties

Capacity int Capacity { get; set; }
Gets or sets the number of elements that the SortedList instance can initially store.
Count int Count { get; }
Gets the number of key/value pairs contained in the SortedList.
Keys ICollection Keys { get; }
Gets an ICollection containing the keys in the SortedList.
Values ICollection Values { get; }
Gets an ICollection containing the values in the SortedList.

Methods

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

Events

This class does not contain any events.

Usage Example

C#


using System;
using System.Collections;

public class Example {
    public static void Main(string[] args) {
        SortedList sl = new SortedList();
        sl.Add("Apple", 1);
        sl.Add("Banana", 2);
        sl.Add("Cherry", 3);

        Console.WriteLine("Count: " + sl.Count);

        Console.WriteLine("Keys:");
        foreach (object key in sl.Keys) {
            Console.WriteLine(" - " + key);
        }

        Console.WriteLine("Values:");
        foreach (object value in sl.Values) {
            Console.WriteLine(" - " + value);
        }

        Console.WriteLine("Value for 'Banana': " + sl["Banana"]);
        Console.WriteLine("Key at index 1: " + sl.GetKey(1));
        Console.WriteLine("Value at index 0: " + sl.GetByIndex(0));
    }
}