Junior Collections

What is a SortedList<TKey, TValue> in C#?

SortedList<TKey, TValue> is a collection of key-value pairs that maintains the

elements sorted by keys.

  • Implements both IDictionary<TKey, TValue> and

IList<KeyValuePair<TKey, TValue>>.

  • Keys are automatically sorted in ascending order based on their natural comparer

or a provided comparer.

Example:

SortedList<int, string> sortedList = new SortedList<int, string>();

sortedList.Add(3, "Three");

sortedList.Add(1, "One");

sortedList.Add(2, "Two");

The elements are stored sorted by key: 1, 2, 3.

More from C# Programming Tutorial

All questions for this course