C# SortedList vs SortedDictionary
Last Updated :
31 Jan, 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:
- SortedList: It uses an array internally which makes it more memory-efficient but slower when comes to insertion and deletion(as elements need to be shifted to maintain order).
- SortedDictionary: It uses a balanced tree structure (like a red-black tree) which allows fast insertion and deletion but at the cost of higher memory usage.
SortedList vs SortedDictionary
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:
- Generic SortedList<Tkey, TValue>: It is defined in the System.Collections.Generic namespace.
- Non-Generic SortedList: It is defined in the System.Collections namespace.
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();
}
}
OutputKey: 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);
}
}
}
OutputSortedDictionary:
Rank 1: Java
Rank 2: JavaScript
Rank 3: C++
Rank 4: C
Rank 5: Ruby