![]() |
VOOZH | about |
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.
key: Geek1, value: 1 key: Geek2, value: 2 key: Geek3, value: 3
In C#, the declaration of SortedDictionary can be done as:
SortedDictionary<TKey, TValue> dictionaryName;
Parameters:
| 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.
Key: 1 and Value: C Key: 2 and Value: C++ Key: 3 and Value: C#
| 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.
Total number of pairs present in SortedDictionary : 4
| 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.
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