Difference between SortedList and SortedDictionary in C#In this article, you will learn about the difference between SortedList and SortedDictionary in C#. But before discussing their differences, you must know about the SortedList and SortedDictionary. What is the SortedList?The C# collection class SortedList represents a collection of key/value pairs with entries ordered by keys. It is part of the System.Collections namespace. SortedList uses a dynamic array to hold its elements. The elements are kept in sorted order based on their keys. Binary search is employed for efficient retrieval of elements based on keys. 1. Key characteristicsSorted Order:
Array-Based Structure
2. PerformanceQuick Access with an Index
Slower Insertions and Removals
3. Memory OverheadLower Memory Overhead
4. Use CasesFrequent Index-Based Access
Relatively Static Collections
Example:Let us take an example to illustrate the SortedList in C#. Output: Sorted List Elements: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three Key: 4, Value: Four Value for Key 2: Two Explanation: 1. Namespace and Class Declaration A namespace (System) and a class (Program) are declared at the start of the code. 2. Main Method The Main method is the program's entry point. 3. Creating a SortedList Using new SortedList(), a new SortedList instance called sortedList is created. 4. Adding Elements Using the Add method, key-value pairs are added to the SortedList. Strings are values, and integers are keys. 5. Sorted Elements Are Displayed After that, the program enters a loop to iterate through the elements of the SortedList. Key-value pairs are accessed using a foreach loop that uses DictionaryEntry as the loop variable type. Based on the keys, the values and keys are arranged in a certain order on the console. 6. Using a Key to Access Elements The program demonstrates how to access elements by key using the indexer ([]) syntax and the ContainsKey method. In this case, it checks if the key 2 exists in the SortedList. If the key is found, it prints the corresponding value; otherwise, it prints a message indicating it was not found. 7. Output The sorted key-value pairs and the result of using a particular key to access an element are displayed in the console output. This code provides a basic example of using a SortedList in C# to store, retrieve, and display sorted key-value pairs. The SortedList class automatically maintains the sorting order based on the keys. What is the SortedDictionary?The C# collection class SortedDictionary represents a collection of key/value pairs, where keys sort the elements. It is part of the System.Collections.Generic namespace. SortedDictionary stores key/value pairs in a Red-Black tree, a self-balancing binary search tree. With logarithmic time complexity, the tree structure ensures effective searching, insertion, and removal operations. 1. Key CharacteristicsSorted Order
Balanced Tree Structure
2. PerformanceFast Insertions and Removals
Slower Access by Index
3. Memory OverheadHigher Memory Overhead
4. Use CasesFrequent Insertions and Removals
Dynamic Collections
Example:Let us take an example to illustrate the SortedDictionary in C#. Output: Sorted Dictionary Elements: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three Key: 4, Value: Four Explanation: 1. Class Declaration and Namespace The Program class is declared at the start of the code. It includes the System and System.Collections.Generic namespaces. 2. Main Method The program's entry point is the Main method. 3. Creating a SortedDictionary The SortedDictionary<int, string> class is generated as a new instance. Key-value pairs with values of type string and keys of type int are stored in SortedDictionary. 4. Adding Elements to SortedDictionary Using the Add method, key-value pairs are added to the SortedDictionary. In this example, the values are strings ("Three," "One," "Two," and "Four"), and the keys are numbers (3, 1, 2, 4). 5. Displaying the Elements The code iterates through the SortedDictionary using a foreach loop. Key and Value properties of each key-value pair (KeyValuePair<int, string>) in the SortedDictionary are accessed to show key-value pairs in sorted order. 6. Output The program produces the sorted list of key-value pairs, displaying the keys and their corresponding values. This code shows how to create an instance, add elements, and loop through the sorted key-value pairs of a SortedDictionary in C#. Based on the keys, the SortedDictionary automatically maintains the sorted order. Difference between SortedList and SortedDictionary in C#The primary difference between SortedList and SortedDictionary in C# lies in their underlying data structures and performance characteristics. 1. Underlying Data StructuresSortedList
SortedDictionary
2. Performance CharacteristicsSortedList
SortedDictionary
3. Memory OverheadSortedList
SortedDictionary
4. Use CasesSortedList
SortedDictionary
5. Index-based AccessSortedList
SortedDictionary
6. Interface ImplementationSortedList
SortedDictionary
7. Operations of Insertion and RemovalSortedList
SortedDictionary
Conclusion:While both SortedList and SortedDictionary offer sorted key-value pairs, their differences in data structures and performance characteristics make them better suited for specific scenarios. SortedList is more efficient for scenarios requiring frequent index-based access in relatively static collections, whereas SortedDictionary excels in situations with dynamic collections that undergo frequent insertions and removals. |