Order Now

C# SortedList

shape image

C# SortedList

C# SortedList


C# SortedList 

C# SortedList is a collection class in the .NET framework that provides a convenient way to store and manage key-value pairs. It is a combination of a List and a Dictionary, as it provides the functionality of both. SortedList is a non-generic class, which means it can store keys and values of any object type.

Benefits of SortedList

One of the main benefits of using SortedList is that it keeps its elements sorted by key, making it easier to manage and search for specific items. The sorting is performed automatically when an item is added or removed from the collection. This means that, unlike a Dictionary, you don't need to sort the items manually.

Another advantage of using SortedList is that it provides fast access to its elements. You can retrieve the value of an item by its key in O(log n) time, making it faster than a List or an array. Additionally, you can access the items in the SortedList by their index, just like in a List. This is particularly useful when you need to iterate over the elements in a specific order.

Limitations of SortedList

However, there are some limitations to using SortedList. For instance, it has a lower performance compared to a Dictionary, especially when dealing with large collections. Additionally, SortedList uses more memory compared to a Dictionary, as it has to maintain both the key-value pairs and the sorted order.

When should you use SortedList?

When deciding whether to use SortedList or another collection, you need to consider your specific requirements. If you need fast access to the elements by key, a sorted collection, and don't mind the slower performance, then SortedList may be the right choice for you. On the other hand, if you need faster performance and don't need the elements to be sorted, then a Dictionary or a HashSet might be a better option.

Namespace of Dictionary
using System.Collections;

Initialization of the SortedList
SortedList list = new SortedList();

Add items to the SortedList
            list.Add("Name", "John");
            list.Add("Age", 30);
            list.Add("City", "New York");

Remove an item from the SortedList
list.Remove("City");

Iterate over the SortedList
foreach (DictionaryEntry entry in list)
{
   Console.WriteLine("Key: {0}, Value: {1}", entry.Key, entry.Value);
}
Note that SortedList is a non-generic class, so we use the DictionaryEntry type when iterating over the collection and accessing its items.

Search for an item in the SortedList
if (list.ContainsKey("Name"))
 {
    Console.WriteLine("Found Name: {0}", list["Name"]);
 }
 else{
   Console.WriteLine("Name not found");
 }


In conclusion, C# SortedList is a collection class that provides the functionality of both a List and a Dictionary, with the added benefit of keeping its elements sorted by the key. It provides fast access to its elements, but may not be the best choice for large collections or when performance is a concern. Consider your specific requirements when choosing between SortedList and other collection classes.

Post a Comment

© Copyright 2023 ZakirDev

Order Form

I will contact you on WhatsApp.

Order now