Open In App

Difference between SortedList and SortedDictionary in C#

Improve
Improve
Like Article
Like
Save
Share
Report

In C#, SortedList is a collection of key/value pairs which are sorted according to keys. By default, this collection sort the key/value pairs in ascending order. It is of both generic and non-generic type of collection. The generic SortedList is defined in System.Collections.Generic namespace whereas non-generic SortedList is defined under System.Collections namespace.

Example:




// C# program to illustrate how
// to create a sortedlist
using System;
using System.Collections;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating a sortedlist
        // Using SortedList class
        SortedList my_Slist = new SortedList();
  
        // Adding key/value pairs in
        // SortedList using Add() method
        my_Slist.Add(1.02, "Dog");
        my_Slist.Add(1.07, "Cat");
        my_Slist.Add(1.04, "Rat");
        my_Slist.Add(1.01, "Bird");
  
        foreach(DictionaryEntry pair in my_Slist)
        {
            Console.WriteLine("{0} and {1}",
                      pair.Key, pair.Value);
        }
        Console.WriteLine();
    }
}


Output:

1.01 and Bird
1.02 and Dog
1.04 and Rat
1.07 and Cat

In C#, SortedDictionary is a generic collection which is used to store the key/value pairs in the sorted form and the sorting is done on the key. SortedDictionary is defined under System.Collection.Generic namespace. It is dynamic in nature means the size of the sorted dictionary is growing according to the need.

Example:




// C# program to illustrate how
// to create a sorted dictionary
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating sorted dictionary
        // Using SortedDictionary class
        SortedDictionary<int, string> My_sdict = 
            new SortedDictionary<int, string>();
  
        // Adding key/value pair in Sorted
        // Dictionary Using Add() method
        My_sdict.Add(004, "Roscosmos");
        My_sdict.Add(003, "ESA");
        My_sdict.Add(001, "NASA");
        My_sdict.Add(005, "ISRO");
        My_sdict.Add(002, "CNSA");
        Console.WriteLine("Top 5 space agencies 2018:");
  
        // Accessing the key/value pair of the
        // SortedDictionary Using foreach loop
        foreach(KeyValuePair<int, string> pair in My_sdict)
        {
            Console.WriteLine("Rank: {0} and Name: {1}",
                                  pair.Key, pair.Value);
        }
    }
}


Output:

Top 5 space agencies 2018:
Rank: 1 and Name: NASA
Rank: 2 and Name: CNSA
Rank: 3 and Name: ESA
Rank: 4 and Name: Roscosmos
Rank: 5 and Name: ISRO

Below are the some differences between SortedList and SortedDictionary:

SortedList SortedDictionary
The memory of SortedList is an overhead. The memory of SortedDictionary is not bottlenecked.
In SortedList, the elements are stored in a continuous block in memory. In SortedDictionary, the elements are stored in separate object that can spread all over the heap.
In SoterdList, the memory fragmentation is high. In SoterdDictionary, the memory fragmentation is low.
It require less memory for storage. It require more memory for storage.
In SortedList, less inserts and delete operations are required. In SortedDictionary, more inserts and delete operations are required.
In SortedList, you can access elements using the index. In SortedDictionary, you can access elements using index or key. Here key access is sufficient there is no need of accessing elements using index.
In SortedList, data are already in sorted form. In SortedDictionary, data are in un-sorted form.


Last Updated : 01 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads