A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/difference-between-sortedlist-and-sorteddictionary-in-c-sharp/ below:

C# SortedList vs SortedDictionary - GeeksforGeeks

C# SortedList vs SortedDictionary

Last Updated : 11 Jul, 2025

In C# both, SortedList and SortedDictionary store key-value pairs in a sorted order. Understanding the main difference between SortedList and SortedDictionary plays a very important role in choosing the right collection for our use case. The main difference between SortedList and SortedDictionary is:

Features

SortedList

SortedDictionary


Underlying Data Structure

SortedList uses an array internally.

SortedDictionary uses a self-balancing tree (like a red-black tree) internally.

Indexed Access

SortedList supports indexed access.

SortedDictionary does not support indexed access.

Key-value Access

Access by index and key

Access only by key

Use Cases

SortedList is suitable for small, static collections.

SortedDictionary is suitable for large, dynamic collections.

Memory Efficiency

SortedList requires less memory.

SortedDictionary requires more memory.

Memory Fragmentation

In SortedList memory fragmentation can be higher because elements are in array.

In SortedDictionary memory fragmentationn is lower because memory is allocated dynamically.

C# SortedList

In C#, SortedList is a collection of key-value pairs, where the keys are automatically sorted in asscending order. It ensues that the key-value pairs remain sorted as we add, remove or modify items in the collection. SortedList is available in both generic and non generic types:

Example: This example demosntrates how to create a non-generic SortedList in C#.

C#
// C# program to illustrate how
// to create a SortedList
using System;
using System.Collections;

class Geeks {

    static public void Main()
    {
        // Creating a non-generic SortedList
        // Using SortedList class
        SortedList sl = new SortedList();

        // Adding key/value pairs in
        // SortedList using Add() method
        sl.Add(4, "C++");
        sl.Add(2, "C#");
        sl.Add(3, "Js");
        sl.Add(1, "Java");

        // Displaying SortedList elements
        foreach(DictionaryEntry e in sl)
        {
            Console.WriteLine("Key: {0}, Value: {1}", e.Key,
                              e.Value);
        }
        Console.WriteLine();
    }
}

Output
Key: 1, Value: Java
Key: 2, Value: C#
Key: 3, Value: Js
Key: 4, Value: C++
C# SortedDictionary

In C# SortedDictionary is a collection that stores key-value pairs in a sorted order based on the keys. SortedDictionary is a generic collection which means we can modify the types for both the keys and values. It is dynamic in nature, the size of the dictionary adjusts dynamically based on the number of elements added or removed. SortedDictionary is available in generic type:

SortedDictionary<TKey, TValue>: It is defined in the System.Collections.Generic namespace.

Example: This example demosntrates how to create a SortedDictionary in C#.

C#
// C# program to demonstrate how to 
// create a SortedDictionary
using System;
using System.Collections.Generic;

class Geeks {
    static void Main()
    {
        // Create a SortedDictionary with int as key
        // and string as value
        SortedDictionary<int, string> sd = new SortedDictionary<int, string>();

        // Add key/value pairs to the SortedDictionary
        sd.Add(4, "C");
        sd.Add(3, "C++");
        sd.Add(1, "Java");
        sd.Add(5, "Ruby");
        sd.Add(2, "JavaScript");

        // Display the elements in the SortedDictionary
        Console.WriteLine("SortedDictionary:");

        // Access key/value pairs using foreach loop
        foreach (KeyValuePair<int, string> entry in sd)
        {
            Console.WriteLine("Rank {0}: {1}", entry.Key, entry.Value);
        }
    }
}

Output
SortedDictionary:
Rank 1: Java
Rank 2: JavaScript
Rank 3: C++
Rank 4: C
Rank 5: Ruby


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4