LogIn
I don't have account.

SortedDictionary<T> in C#

DevSniper
118 Views

Namespace :- System.Collections.Generic
Assembly :- System.Collections.dll
Signature :-
public class SortedDictionary<TKey,TValue> :
System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>,
System.Collections.Generic.IDictionary<TKey,TValue>,
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>,
System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>,
System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>,
System.Collections.IDictionary
  • It is used to store key/value pairs in the sorted order of key
  • it does not store duplicate key. key must be unique.
  • Key can not be null
  • Value can be null in the case of reference type
  • It allowed only the same type of key/value pair data
  • Generic implementation of SortedDictionary<TKey,TValue> class is a binary search tree (BST). With retrieval complexity O(log n)
  • SortedDictionary<TKey,TValue> requires higher memory than SortedList<TKey,TValue>.
  • For unsorted data , SortedDictionary<TKey,TValue> performs faster insertion and removal operations in O(log n) compared to SortedList<TKey,TValue> which performs these operations in O(n)
  • SortedList<TKey,TValue> is faster than SortedDictionary<TKey,TValue> if list is created all at once from sorted data.
SortedDictionary<TKey,TValue> Constructors
  1. public SortedDictionary ();
  2. public SortedDictionary (System.Collections.Generic.IComparer<TKey>? comparer);
  3. public SortedDictionary (System.Collections.Generic.IDictionary<TKey,TValue> dictionary);
  4. public SortedDictionary (System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey>? comparer);
SortedDictionary<TKey,TValue> Properties
  • Comparer :- public System.Collections.Generic.IComparer<TKey> Comparer { get; }
    Gets the IComparer<T> that is used to sort/order element of the SortedDictionary<TKey,TValue>.
  • Count :- public int Count { get; }
    Gets the number of key/value pairs that is present in the SortedDictionary<TKey,TValue>. it is an O(1) operation.
  • Item[TKey] :- public TValue this[TKey key] { get; set; }
    Gets or sets the value linked with the specified key. Getting and setting the value of this property, both is an O(log n) operation.
  • Keys :- public System.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection Keys { get; }
    Gets a KeyCollection containing the keys in the SortedDictionary<TKey,TValue>. it is also an O(1) operation.
  • Values :- public System.Collections.Generic.SortedDictionary<TKey,TValue>.ValueCollection Values { get; }
    Gets a ValueCollection containing the values in the SortedDictionary<TKey,TValue>. it is an O(1) operation.
Creating and adding element in SortedDictionary<TKey,TValue>
1. initialize the SortedDictionary<TKey,TValue> with initial values
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var dict = new SortedDictionary<string,string> {
            {"language","C#"},
            {"topic","SortedDictionary"},
            {"label","Beginner"}
        };
        Console.WriteLine("We are learning "+dict["language"]+" "+dict["topic"] +" and level : "+dict["label"]);
         Console.WriteLine("------------------loop------------");
        foreach (KeyValuePair<string, string> kv in dict) {
            Console.WriteLine("K: "+ kv.Key+" V: "+kv.Value);
        }
    }
}
We are learning C# SortedDictionary and level : Beginner
------------------loop------------
K: label V: Beginner
K: language V: C#
K: topic V: SortedDictionary
2. Direct Assign to add new element in SortedDictionary<TKey,TValue>
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var dict = new SortedDictionary<string,string>();
        dict["language"]="C#";
        dict["topic"]="SortedDictionary";
        dict["label"]="Beginner";
        Console.WriteLine("We are learning "+dict["language"]+" "+dict["topic"] +" and level : "+dict["label"]);
        Console.WriteLine("------------------loop------------");
        foreach (KeyValuePair<string, string> kv in dict) {
            Console.WriteLine("K: "+ kv.Key+" V: "+kv.Value);
        }
    }
}
We are learning C# SortedDictionary and level : Beginner
------------------loop------------
K: label V: Beginner
K: language V: C#
K: topic V: SortedDictionary
3. Using Add() Method to add new element in SortedDictionary<TKey,TValue>
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var dict = new SortedDictionary<string,string>();
        dict.Add("language","C#");
        dict.Add("topic","SortedDictionary");
        dict.Add("label","Beginner");
        Console.WriteLine("We are learning "+dict["language"]+" "+dict["topic"] +" and level : "+dict["label"]);
        Console.WriteLine("------------------loop------------");
        foreach (KeyValuePair<string, string> kv in dict) {
            Console.WriteLine("K: "+ kv.Key+" V: "+kv.Value);
        }
    }
}
We are learning C# SortedDictionary and level : Beginner
------------------loop------------
K: label V: Beginner
K: language V: C#
K: topic V: SortedDictionary