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
SortedMap Interface in C#
In C#, there is no direct equivalent to Java's SortedMap interface. However, C# provides the SortedList and SortedDictionary collections which offer similar functionality for maintaining key-value pairs in sorted order.
The SortedList collection in C# uses both a key and an index to access items. It combines the features of an array and a hash table, maintaining items in sorted order based on the key values. You can access items either by index (like an ArrayList) or by key (like a Hashtable).
Syntax
Following is the syntax for creating a SortedList −
SortedList sortedList = new SortedList(); SortedList<TKey, TValue> sortedList = new SortedList<TKey, TValue>();
Following is the syntax for adding and accessing elements −
sortedList.Add(key, value); var value = sortedList[key];
Using SortedList with Keys
The following example demonstrates how to create a SortedList, add items, and display the keys −
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
SortedList sl = new SortedList();
sl.Add("ST2", "Three");
sl.Add("ST0", "One");
sl.Add("ST1", "Two");
ICollection keys = sl.Keys;
Console.WriteLine("Keys in sorted order:");
foreach(string k in keys) {
Console.WriteLine(k);
}
}
}
The output of the above code is −
Keys in sorted order: ST0 ST1 ST2
Using Generic SortedList
The generic version provides type safety and better performance −
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
SortedList<string, string> sl = new SortedList<string, string>();
sl.Add("C", "Cat");
sl.Add("A", "Apple");
sl.Add("B", "Ball");
Console.WriteLine("Key-Value pairs in sorted order:");
foreach(KeyValuePair<string, string> pair in sl) {
Console.WriteLine($"{pair.Key}: {pair.Value}");
}
Console.WriteLine($"\nAccessing by key 'B': {sl["B"]}");
Console.WriteLine($"Accessing by index 1: {sl.Values[1]}");
}
}
The output of the above code is −
Key-Value pairs in sorted order: A: Apple B: Ball C: Cat Accessing by key 'B': Ball Accessing by index 1: Ball
SortedList vs SortedDictionary
| SortedList | SortedDictionary |
|---|---|
| Uses less memory | Uses more memory |
| Faster retrieval by index | No index-based access |
| Slower insertion/deletion O(n) | Faster insertion/deletion O(log n) |
| Better for frequent lookups | Better for frequent updates |
Common Use Cases
SortedList is ideal when you need −
-
Automatic sorting of key-value pairs
-
Access by both key and index
-
Memory-efficient storage
-
Frequent lookups with occasional updates
Conclusion
While C# doesn't have Java's SortedMap interface, SortedList provides equivalent functionality for maintaining sorted key-value collections. It offers dual access methods and automatic sorting, making it suitable for scenarios requiring ordered data with efficient lookups.
