C# SortedDictionary Class
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# 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}");
}
}
}
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
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# 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);
}
}
}
Output
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# 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);
}
}
Output
Total 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# 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);
}
}
}
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