A C# SortedList class represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index. The SortedListvclass is included in the System.Collection namespace.
A SortedList object internally manages two arrays to store the elements of the list; that is, one array for the keys and another array for values. Each element is a key/value pair that can be accessed as a DictionaryEntry object. A key cannot be null but a value can be.
SortedList has 6 different types of constructor like SortedList(), SortedList(IComparer), etc.
In the SortedList index, the sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly.
When an element is removed, the indexing also adjusts accordingly. Therefore, the index of a specific key/value pair might change as elements are added or removed from the SortedList object.
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
The foreach statement of the C# language returns an object of the type of elements in the collection. Since each element of the SortedList object is a key/value pair, the element type is not the type of the key or the type of the value. Rather, the element type is DictionaryEntry.
C# SortedList Properties
|
Keys
|
Gets the keys contained in the SortedList object.
|
|
Values
|
Gets the keys contained in the theSortedList object.
|
|
Item[Object]
|
Gets or sets the value associated with a specific key in a SortedList object.(object=key)
|
|
Capacity
|
Gets or sets the capacity of a SortedList object.
|
|
Count
|
Gets the number of elements contained in a SortedList object.
|
|
IsFixedSize
|
Gets bool value the SortedList is fixed size.
|
|
IsReadOnly
|
Gets a value indicating whether a SortedList object is read-only.
|
C# SortedList Methods
|
Add(Object, Object)
|
Adds an element with the specified key and value to a SortedList object.
|
|
Clone()
|
Creates a shallow copy of a SortedList object.
|
|
Contains(Object)
|
Return bool value, Determines whether the SortedList contains a specific key.
|
|
ContainsKey(Object)
|
Return bool value, Determines whether the SortedList contains a specific key.
|
|
ContainsValue(Object)
|
Return bool value, Determines whether the SortedList contains a specific value.
|
|
IndexOfKey(Object)
|
Returns the zero-based index of the specified key in a SortedList object.
|
|
IndexOfValue(Object)
|
Returns the zero-based index of the first occurrence of the specified value in a SortedListobject.
|
Example 1
The following code snippet creates a SortedList. where the key type is int and value type is a string.
- SortedList NumberSortedList = new SortedList();
- //The following code snippet adds items(unordered) to the SortedList.
- NumberSortedList.Add(4, "Four");
- NumberSortedList.Add(1, "One");
- NumberSortedList.Add(3, "Three");
- NumberSortedList.Add(7, "Seven");
- NumberSortedList.Add(5, "Five");
- NumberSortedList.Add(6, "Six");
- NumberSortedList.Add(2, "Two");
- Console.WriteLine("--Number SortedList(sorted by the keys)--");
- //The SortedList is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.
- foreach (DictionaryEntry item in NumberSortedList)
- {
- Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
- }
Output

Example 2
The following code snippet creates a SortedList where the value type is List<string>.







Amit MohantyPosted Jul 15, 2019, 12:30 AM
Nice article
Ano MepaniPosted Jul 13, 2019, 11:26 PM
Nice article. Very well detailed example for sortedlist
Rajanikant HawaldarPosted Jul 13, 2019, 10:48 AM
nice article, instead of Console.WriteLine("") use \n