C# SortedList with Examples
In C#, SortedList is a collection of key/value pairs which are sorted according to keys. By default, this collection sort the key/value pairs in ascending order. It is of both generic and non-generic type of collection. The generic SortedList is defined in System.Collections.Generic namespace whereas non-generic SortedList is defined under System.Collections namespace, here we will discuss non-generic type SortedList.
- The SortedList class implements the IEnumerable, ICollection, IDictionary and ICloneable interfaces.
- In SortedList, an element can be accessed by its key or by its index.
- A SortedList object internally maintains two arrays to store the elements of the list, i.e, one array for the keys and another array for the associated values.
- Here, a key cannot be null, but a value can be.
- The capacity of a SortedList object is the number of key/value pairs it can hold.
- In SortedList, duplicate keys are not allowed.
- In SortedList, you can store values of the same type and of the different types due to the non-generic collection. If you use a generic SortedList in your program, then it is necessary that the type of the values should be the same.
- In SortedList you cannot store keys of different data types in the same SortedList because the compiler will throw an exception. So, always add the key in your SortedList of the same type.
- You can also cast key/value pair of SortedList into DictionaryEntry.
How to create a SortedList?
The SortedList class provides 6 different types of constructors which are used to create a SortedList, here we only use SortedList(), constructor. To read more about SortedList’s constructors you can refer to C# | SortedList Class.
SortedList(): It is used to create an instance of the SortedList class that is empty, has the default initial capacity, and is sorted according to the IComparable interface implemented by each key added to the SortedList object.
Step 1: Include System.Collections namespace in your program with the help of using keyword:
Step 2: Create a SortedList using SortedList class as shown below:
SortedList list_name = new SortedList();
Step 3: If you want to add a key/value pair in your SortedList, then use Add() method to add key/value pairs in your SortedList. And you can also store a key/value pair in your SortedList without using the Add() method, this type of syntax is known as Collection Initializer Syntax as shown in the below example.
Step 4: The key/value pairs of the SortedList is accessed using three different ways:
- for loop: You can use for loop to access the key/value pairs of the SortedList.
- Using Index: You can access the individual value of the SortedList by using the index. You need to pass the key or index as a parameter to find the respective value. If the specified key is not available, then the compiler will throw an error.
- foreach loop: You can use a foreach loop to access the key/value pairs of the SortedList.
1.01 and Tutorial 1.02 and This 1.04 and SortedList 1.07 and Is b.01 and 405 b.09 and 234 b.11 and 395 b.67 and 100
How to remove elements from the SortedList?
- Clear: This method is used to removes all elements from a SortedList object.
- Remove: This method is used to removes the element with the specified key from a SortedList object.
- RemoveAt: This method is used to removes the element at the specified index of a SortedList object.
1.01 and Tutorial 1.02 and This 1.04 and SortedList 1.07 and Is 1.01 and Tutorial 1.02 and This 1.04 and SortedList 1.01 and Tutorial 1.02 and This The total number of key/value pairs present in my_slist:0
How to check the availability of key/value pair in SortedList?
In SortedList, you can check whether the given pair is present or not using the following methods:
- Contains: This method is used to check whether a SortedList object contains a specific key.
- ContainsKey: This method is used to check whether a SortedList object contains a specific key.
- ContainsValue: This method is used to check whether a SortedList object contains a specific value.
Key is found...!! Key is not found...!! Value is found...!!