This documentation is archived and is not being maintained.

SortedDictionary(TKey, TValue) Class

Represents a collection of key/value pairs that are sorted on the key.

Namespace:  System.Collections.Generic
Assembly:  System (in System.dll)

JScript does not support generic types or methods.

Type Parameters


The type of the keys in the dictionary.


The type of the values in the dictionary.

The SortedDictionary(TKey, TValue) generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this respect, it is similar to the SortedList(TKey, TValue) generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:

Each key/value pair can be retrieved as a KeyValuePair(TKey, TValue) structure, or as a DictionaryEntry through the nongeneric IDictionary interface.

Keys must be immutable as long as they are used as keys in the SortedDictionary(TKey, TValue). Every key in a SortedDictionary(TKey, TValue) must be unique. A key cannot be a null reference (Nothing in Visual Basic), but a value can be, if the value type TValue is a reference type.

SortedDictionary(TKey, TValue) requires a comparer implementation to perform key comparisons. You can specify an implementation of the IComparer(T) generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic comparer Comparer(T).Default is used. If type TKey implements the System.IComparable(T) generic interface, the default comparer uses that implementation.

The foreach statement of the C# language (for each in C++, For Each in Visual Basic) requires the type of each element in the collection. Since each element of the SortedDictionary(TKey, TValue) is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is KeyValuePair(TKey, TValue). The following code shows C#, C++, and Visual Basic syntax.

No code example is currently available or this language may not be supported.

The foreach statement is a wrapper around the enumerator, which allows only reading from the collection, not writing to it.

The following code example creates an empty SortedDictionary(TKey, TValue) of strings with string keys and uses the Add method to add some elements. The example demonstrates that the Add method throws an ArgumentException when attempting to add a duplicate key.

The example uses the Item property (the indexer in C#) to retrieve values, demonstrating that a KeyNotFoundException is thrown when a requested key is not present, and showing that the value associated with a key can be replaced.

The example shows how to use the TryGetValue method as a more efficient way to retrieve values if a program often must try key values that are not in the dictionary, and it shows how to use the ContainsKey method to test whether a key exists before calling the Add method.

The example shows how to enumerate the keys and values in the dictionary and how to enumerate the keys and values alone using the Keys property and the Values property.

Finally, the example demonstrates the Remove method.

No code example is currently available or this language may not be supported.

  System.Collections.Generic.SortedDictionary(TKey, TValue)

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

A SortedDictionary(TKey, TValue) can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0