LogIn
I don't have account.

SortedList <TKey, TValue> in C#

DevSniper
120 Views

Namespace :- System.Collections.Generic
Assembly :- System.Collections.dll
Signature :-
public class SortedList<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
  • By default , It will store data in ascending of key
  • it does not store duplicate key. key must be unique.
  • Value can be null in the case of reference type
  • It allowed only the same type of key/value pair data
  • Generic class SortedList<TKey,TValue> is an array of key/value pairs. With retrieval/get complexity O(log n)
  • SortedList<TKey,TValue> uses less memory than SortedDictionary<TKey,TValue>.
  • SortedDictionary<TKey,TValue> and SortedList<TKey,TValue> both has o( log n) time complexity for getting/retrieval of data
  • SortedList<TKey,TValue> is faster than SortedDictionary<TKey,TValue> if list is created all at once from sorted data.
  • 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).
  • The capacity of SortedList<TKey,TValue> is a number of element that SortedList<TKey,TValue> can hold. Whenever we add a new element in the SortedList<TKey,TValue>the capacity is automatically increased as required by reallocating the internal array.
  • Capacity of the SortedList<TKey,TValue> can be decreased by calling TrimExcess or by setting the Capacity property explicitly.whenever we decrease the capacity, This process will reallocates memory and copies all the elements in the SortedList<TKey,TValue>.
SortedList<TKey, TValue> Constructors
  1. public SortedList ();
  2. public SortedList (System.Collections.Generic.IComparer<TKey>? comparer);
  3. public SortedList (System.Collections.Generic.IDictionary<TKey,TValue> dictionary);
  4. public SortedList (System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey>? comparer);
  5. public SortedList (int capacity);
  6. public SortedList (int capacity, System.Collections.Generic.IComparer<TKey>? comparer);
SortedList <TKey, TValue> Properties
  • Capacity :- public int Capacity { get; set; }
    Gets the number of elements that SortedList<TKey,TValue> can store. getting the value of capacity is O(1) operation and setting the capacity property is O(n) operation.
  • Comparer :- public System.Collections.Generic.IComparer<TKey> Comparer { get; }
    Gets the IComparer<TKey> that is used to sort/order element of the SortedList<TKey,TValue>.
  • Count :- public int Count { get; }
    Gets the number of key/value pairs that is present in the SortedList<TKey, TValue>. It is an O(1) operation.
  • Item[TKey] :- public TValue this[TKey key] { get; set; }
    Gets or sets the value associated with the specified key. Getting the value by this property is an o( log n) operation . Setting the value in the SortedList<TKey,TValue> O( log n) operation if key already exist in the SortedList<TKey,TValue>. if key not present in the SortedList<TKey,TValue> setting will be O( n ) operation for unsorted data OR if the new element added at the end of the SortedList<TKey,TValue> this will be an O( log n) operation. if adding a new element require array resizing then this will be an O(n) operation.
  • Keys :- public System.Collections.Generic.IList<TKey> Keys { get; }
    Gets a IList<TKey> containing the keys in the SortedList <TKey, TValue> . it is also an O(1) operation.
  • Values :- public System.Collections.Generic.IList<TValue> Values { get; }
    Gets a IList<TValue> containing the values in the SortedList<TKey, TValue>. it is an O(1) operation.
Creating and adding element in SortedList<TKey, TValue>
1. initialize the SortedList<TKey,TValue> with initial values
using System;
using System.Collections.Generic;

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

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

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