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.
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}");
}
}
}
key: Geek1, value: 1 key: Geek2, value: 2 key: Geek3, value: 3Declarartion of SortedDictionary
In C#, the declaration of SortedDictionary can be done as:
SortedDictionary<TKey, TValue> dictionaryName;
Parameters:
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);
}
}
}
Key: 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);
}
}
Total number of pairs present in SortedDictionary : 4Methods 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);
}
}
}
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4