VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/sorteddictionary-class-in-c-sharp/

⇱ C# SortedDictionary Class - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# SortedDictionary Class

Last Updated : 11 Jul, 2025

In C#, the SortedDictionary<TKey,TValue> class represents the collection of key/value pairs. This pair is in sorted form and the sorting is done on the key. This class is defined under System.Collections.Generic namespace.

  • In the SortedDictionary class, the keys are immutable, always unique, and cannot be null. We are allowed to use null in value if the type of value is of reference type.
  • The SortedDictionary class provides the fastest insertion and removal operations for unsorted data.
  • The key/value pair of the SortedDictionary class is retrieved by using the KeyValuePair structure.

Example: This example, demonstrates how to create a SortedDictionary, insert key-value pairs, and iterate through it to display the sorted key-value pairs.


Output
key: Geek1, value: 1
key: Geek2, value: 2
key: Geek3, value: 3

Declarartion of SortedDictionary

In C#, the declaration of SortedDictionary can be done as:

SortedDictionary<TKey, TValue> dictionaryName;

Parameters:

  • TKey: Denotes the type of the keys in the dictionary.
  • TValue: Denotes the type of the value in the dictionary.

Constructors

ConstructorsDescription
SortedDictionary<TKey,TValue>()Initializes a new instance of the SortedDictionary class that is empty and uses the default IComparer implementation for the key type.
SortedDictionary<TKey,TValue>(IComparer)Initializes a new instance of the SortedDictionary class that is empty and uses the specified IComparer implementation to compare keys.
SortedDictionary<TKey,TValue>(IDictionary)Initializes a new instance of the SortedDictionary class that contains elements copied from the specified IDictionary and uses the default IComparer implementation for the key type.
SortedDictionary<TKey,TValue>(IDictionary, IComparer)Initializes a new instance of the SortedDictionary class that contains elements copied from the specified IDictionary and uses the specified IComparer implementation to compare keys.


Example: This example demonstrates how to create a Sortedlist, add elements and iterate over the elements where the keys are integer and values are string.


Output
Key: 1 and Value: C
Key: 2 and Value: C++
Key: 3 and Value: C#

Properties

PropertiesDescription
ComparerGets the IComparer used to order the elements of the SortedDictionary.
CountGets the number of key/value pairs contained in the SortedDictionary.
Item[TKey]Gets or sets the value associated with the specified key.
KeysGets a collection containing the keys in the SortedDictionary.
ValuesGets a collection containing the values in the SortedDictionary.


Example: This example demonstrates how to use the Count property.


Output
Total number of pairs present in SortedDictionary : 4

Methods

MethodsDescription
Add(TKey, TValue)Adds an element with the specified key and value into the SortedDictionary.
Clear()Removes all elements from the SortedDictionary.
ContainsKey(TKey)Determines whether the SortedDictionary contains an element with the specified key.
ContainsValue(TValue)Determines whether the SortedDictionary contains an element with the specified value.
CopyTo(KeyValuePair<TKey,TValue>[], Int32)Copies the elements of the SortedDictionary to the specified array of KeyValuePair structures, starting at the specified index.
Equals(Object)Determines whether the specified object is equal to the current object.
GetEnumerator()Returns an enumerator that iterates through the SortedDictionary.
GetHashCode()Serves as the default hash function.
GetType()Gets the Type of the current instance.
MemberwiseClone()Creates a shallow copy of the current Object.
Remove(TKey)Removes the element with the specified key from the SortedDictionary.
ToString()Returns a string that represents the current object.
TryGetValue(TKey, TValue)Gets the value associated with the specified key.

Example: This example demonstrates how to remove key-value pair from a SortedDictionary.


Output
SortedDictionary before removal:
Key: 1, Value: Geek1
Key: 2, Value: Geek2
Key: 3, Value: Geek3

SortedDictionary after removal:
Key: 1, Value: Geek1
Key: 3, Value: Geek3
Comment

Explore