Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between SortedList and SortedDictionary in C#
Both SortedList and SortedDictionary in C# are generic collections that store key-value pairs in sorted order based on the key. However, they differ significantly in their internal implementation, memory usage, and performance characteristics.
Understanding these differences helps you choose the right collection for your specific use case based on performance requirements and usage patterns.
Syntax
Following is the syntax for declaring a SortedList −
SortedList<TKey, TValue> sortedList = new SortedList<TKey, TValue>();
Following is the syntax for declaring a SortedDictionary −
SortedDictionary<TKey, TValue> sortedDictionary = new SortedDictionary<TKey, TValue>();
Key Differences
| Aspect | SortedList | SortedDictionary |
|---|---|---|
| Internal Structure | Uses two arrays (keys and values) stored contiguously | Uses a binary search tree (red-black tree) |
| Memory Usage | Lower memory overhead | Higher memory overhead due to node objects |
| Index Access | Supports access by index (O(1)) | No index-based access |
| Insertion Performance | O(n) - requires shifting elements | O(log n) - tree insertion |
| Lookup Performance | O(log n) - binary search | O(log n) - tree traversal |
| Best Use Case | More lookups than insertions/deletions | Frequent insertions and deletions |
Using SortedList
Example
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
SortedList<string, int> sortedList = new SortedList<string, int>();
// Adding elements
sortedList.Add("Charlie", 30);
sortedList.Add("Alice", 25);
sortedList.Add("Bob", 35);
Console.WriteLine("SortedList contents:");
foreach (var kvp in sortedList) {
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
// Index-based access
Console.WriteLine($"\nFirst item: {sortedList.Keys[0]} = {sortedList.Values[0]}");
Console.WriteLine($"Count: {sortedList.Count}");
}
}
The output of the above code is −
SortedList contents: Alice: 25 Bob: 35 Charlie: 30 First item: Alice = 25 Count: 3
Using SortedDictionary
Example
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
SortedDictionary<string, int> sortedDict = new SortedDictionary<string, int>();
// Adding elements
sortedDict.Add("Charlie", 30);
sortedDict.Add("Alice", 25);
sortedDict.Add("Bob", 35);
Console.WriteLine("SortedDictionary contents:");
foreach (var kvp in sortedDict) {
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
// Key-based access only
Console.WriteLine($"\nAlice's age: {sortedDict["Alice"]}");
Console.WriteLine($"Count: {sortedDict.Count}");
}
}
The output of the above code is −
SortedDictionary contents: Alice: 25 Bob: 35 Charlie: 30 Alice's age: 25 Count: 3
Performance Comparison Example
Example
using System;
using System.Collections.Generic;
using System.Diagnostics;
class Program {
public static void Main() {
const int iterations = 100000;
// SortedList performance
var sortedList = new SortedList<int, string>();
var sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++) {
sortedList[i] = $"Value{i}";
}
sw.Stop();
Console.WriteLine($"SortedList insertion: {sw.ElapsedMilliseconds} ms");
// SortedDictionary performance
var sortedDict = new SortedDictionary<int, string>();
sw.Restart();
for (int i = 0; i < iterations; i++) {
sortedDict[i] = $"Value{i}";
}
sw.Stop();
Console.WriteLine($"SortedDictionary insertion: {sw.ElapsedMilliseconds} ms");
Console.WriteLine($"\nSortedList memory (approx): Lower");
Console.WriteLine($"SortedDictionary memory (approx): Higher");
}
}
The output of the above code is −
SortedList insertion: 156 ms SortedDictionary insertion: 31 ms SortedList memory (approx): Lower SortedDictionary memory (approx): Higher
When to Use Which
Use SortedList when:
You need index-based access to elements
Memory usage is a concern
You perform more lookups than insertions/deletions
You need to access elements by position
Use SortedDictionary when:
You frequently insert and delete elements
You only need key-based access
Insertion performance is more important than memory usage
You're working with large datasets with frequent modifications
Conclusion
SortedList offers better memory efficiency and index-based access but slower insertions, while SortedDictionary provides faster insertions and deletions using a tree structure. Choose SortedList for read-heavy scenarios and SortedDictionary for write-heavy operations with frequent modifications.
