C# SortedDictionary Class
Last Updated :
03 Feb, 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.
C#
// C# Program to add elements in a SortedDictionary
using System;
using System.Collections.Generic;
class Geeks {
static void Main()
{
// Create a SortedDictionary to store key-value
// pairs
SortedDictionary<string, int> sd
= new SortedDictionary<string, int>();
// Insert elements into the SortedDictionary
sd.Add("Geek1", 1);
sd.Add("Geek2", 2);
sd.Add("Geek3", 3);
// Display all key-value pairs in the
// SortedDictionary
foreach(var i in sd)
{
Console.WriteLine(
$"key: {i.Key}, value: {i.Value}");
}
}
}
Outputkey: 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
Constructors | Description |
---|
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.
C#
// C# program to demonstrate the
// concept of SortedDictionary
using System;
using System.Collections.Generic;
public class Geeks {
static public void Main()
{
// Create a new SortedDictionary
// of strings, with int keys
SortedDictionary<int, string> sd =
new SortedDictionary<int, string>();
// Adding key/value pairs
sd.Add(1, "C");
sd.Add(2, "C++");
sd.Add(3, "C#");
// Display the key/value pairs
foreach(KeyValuePair<int, string> pair in sd)
{
Console.WriteLine("Key: {0} and Value: {1}",
pair.Key, pair.Value);
}
}
}
OutputKey: 1 and Value: C
Key: 2 and Value: C++
Key: 3 and Value: C#
Properties
Properties | Description |
---|
Comparer | Gets the IComparer used to order the elements of the SortedDictionary. |
Count | Gets the number of key/value pairs contained in the SortedDictionary. |
Item[TKey] | Gets or sets the value associated with the specified key. |
Keys | Gets a collection containing the keys in the SortedDictionary. |
Values | Gets a collection containing the values in the SortedDictionary. |
Example: This example demonstrates how to use the Count property.
C#
// C# program to demonstrates the concept
// of Count property in SortedDictionary
using System;
using System.Collections.Generic;
public class Geeks {
static public void Main()
{
// Create a new SortedDictionary
// of strings, with int keys
SortedDictionary<int, string> sd =
new SortedDictionary<int, string>();
// Adding key/value pairs
sd.Add(1, "Geek1");
sd.Add(2, "Geek2");
sd.Add(3, "Geek3");
sd.Add(4, "Geek4");
// Display the total number of
// key/value pairs present
Console.WriteLine("Total number of pairs "+
"present in SortedDictionary : {0}", sd.Count);
}
}
OutputTotal number of pairs present in SortedDictionary : 4
Methods
Methods | Description |
---|
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.
C#
// C# program to demonstrates how
// to remove elements in SortedDictionary
using System;
using System.Collections.Generic;
public class Geeks {
static public void Main()
{
// Create a new SortedDictionary of int keys and
// string values
SortedDictionary<int, string> sd
= new SortedDictionary<int, string>();
// Adding key/value pairs to the SortedDictionary
sd.Add(1, "Geek1");
sd.Add(2, "Geek2");
sd.Add(3, "Geek3");
// Display the SortedDictionary before removal
Console.WriteLine(
"SortedDictionary before removal:");
foreach(KeyValuePair<int, string> pair in sd)
{
Console.WriteLine("Key: {0}, Value: {1}",
pair.Key, pair.Value);
}
// Remove an element from the SortedDictionary
sd.Remove(2);
// Display the SortedDictionary after removal
Console.WriteLine(
"\nSortedDictionary after removal:");
foreach(KeyValuePair<int, string> pair in sd)
{
Console.WriteLine("Key: {0}, Value: {1}",
pair.Key, pair.Value);
}
}
}
OutputSortedDictionary 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
Similar Reads
C# OrderedDictionary Class
In C#, the OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System.Collections.Specialized namespace. It implements both IDicitonary and ICollection interfaces.Allows indexed access to elements via both keys and numeric inde
6 min read
C# ListDictionary Class
In C#, the ListDictionary class is the part of the System.Collections.Specialized namespace. It is a collection that stores key-value pairs. It is used in scenarios where the number of elements is relatively small and the order of insertion needs to be preserved. It uses a single LinkedList to store
5 min read
C# Dictionary Class
In C#, the Dictionary class is the part of the System.Collections.Generic namespace. It is a Collection that stores Key-value pairs. Each key in the dictionary is unique and each key maps to a single value. In Dictionary, each entry consists of a key and its associated value.It provides constant tim
7 min read
C# HybridDictionary Class
In C#, the HybridDictionary Class is the part of the System.Collections.Specialized namespace. It is a collection that combines the features of both a Hashtable and a ListDictionary. It implements a linked list and hash table data structure. It implements IDictionary by using a ListDictionary when t
6 min read
C# StringDictionary Class
In C#, the StringDictionary class is the part of the System.Collections.Specialized namespace. It is a collection of key-value pairs where the keys are strings and values are objects. It is similar to a Hashtable. But it is specifically designed for use with string keys. Keys are always strings, and
5 min read
C# | SortedDictionary.Clear() Method
This method is used to remove all key/value pairs from the SortedDictionary<TKey, TValue>. Syntax: public void Clear (); Below are the programs to illustrate the use of the above-discussed method: Example 1: // C# code to remove all pairs // from SortedDictionary using System; using System.Col
2 min read
C# SortedList Class
SortedList class in C# is a collection of key-value pairs that are sorted by keys. By default, it sorts the key-value pairs in ascending order. It is both a generic and non-generic type, of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic
6 min read
C# SortedSet Class
SortedSet class in C# represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. In C#, the SortedSet class can be used to store, remove, or view elements.It maintains ascending order and does not store duplicate elements.It is suggested to
6 min read
C# | SortedDictionary.Add() Method
This is used to add a specified key and value to the sorted dictionary. The elements are sorted according to TKey. Syntax: public void Add (TKey key, TValue value); Parameters: key: It is the key of the element to add. value: It is the value of the element to add. The value can be null for reference
3 min read
C# | SortedDictionary.Keys Property
This property is used to get a collection containing the keys in the SortedDictionary. Syntax: public System.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection Keys { get; } Return Value : It returns a collection containing the keys in the SortedDictionary. Below are the programs
2 min read