Updated April 12, 2023
Introduction to C# SortedList
A collection of pairs of keys and values in which the sorting is done according to the keys is called SortedList in C# in which the sorting is done in ascending order by default and the collection is of both generic and non-generic type collection and the System.Collections.Generic namespace defines the generic sorted list and System. Collections namespace defines the non-generic sorted list and IEnumerable, ICollection, IDictionary, and IClonable interfaces are implemented by sorted list class and any element in the sorted list is identified by its index or keys and the object of the sorted list maintains two arrays internally to store the elements of the list in which one array is used to store the keys and the other array is used to store the values associated with the keys.
Syntax:
SortedListlist_name = new SortedList();
Where list_name is the name of the list.
Working of SortedList in C#
- A collection of pairs of keys and values that are sorted in ascending order by default based on the keys and is identified by keys or indexes is called SortedList in C#.
- Two arrays are maintained by the SortedList internally among which one array is used to store the keys and the other array is used to store the values associated with the keys.
- A key can never be null whereas the value can be null.
- The number of elements held by the SortedList is the capacity of the SortedList.
- Duplicate keys are not allowed in the SortedList.
- As the elements of the SortedList will be sorted, the operations performed on the SortedList are slower.
- An integer index can be used to access the elements in the collection.
Examples of C# SortedList
Below are the examples of SortedList C#:
Example #1
C# program to create a SortedList and to check if the object of the SortedList had a fixed size or no and also to check if the SortedList is read only or not.
Code:
using System;
using System.Collections;
//a class called program is created
class program
{
// main method is called
public static void Main()
{
// a sorted list is created
SortedList List = new SortedList();
//Adding the keys and values pairs to the created sorted list
List.Add("10", "Shobha");
List.Add("20", "Ramya");
List.Add("30", "Rahul");
List.Add("40", "Bhuvan");
List.Add("50", "Kiran");
//Displaying the elements of the sorted list by using keys
for (int r = 0; r <List.Count; r++)
{
Console.WriteLine("{0} and {1}",
List.GetKey(r),
List.GetByIndex(r));
}
// checking if the sorted list has a fixed size or no
Console.WriteLine(List.IsFixedSize);
//checking if the sorted list is read only or not
Console.WriteLine(List.IsReadOnly);
}
}
Output:
Explanation: In the above program, a class called program is created. Then the main method is called. Then a new sorted list is created. Then elements are added to the newly created sorted list in the form of keys and values pairs. Then the elements of the sorted list are displayed by using the keys. Then by using a property of sorted list, the sorted list is checked if it has a fixed size or no. Then by using a property of sorted list, the sorted list is checked if it is read-only or not. The output of the program is shown in the snapshot above.
Example #2
C# program to create a sorted list and to display the number of elements in the sorted list and to display the capacity of the sorted list and to clear all the elements of the sorted list.
Code:
using System;
using System.Collections;
//a class called program is created
class program
{
// main method is called
public static void Main()
{
//a sorted list is created
SortedList List = new SortedList();
// Adding elements to SortedList
List.Add("10", "Shobha");
List.Add("20", "Ramya");
List.Add("30", "Rahul");
List.Add("40", "Bhuvan");
List.Add("50", "Kiran");
//the number of elements in the newly created sorted list is displayed
Console.WriteLine("The count of the elements in the sorted list is : "
+ List.Count);
//the capacity of the newly created sorted list is displayed
Console.WriteLine("The newly created sorted list's capacity is : "
+ List.Capacity);
//Deleting all the elements from the newly created sorted list
List.Clear();
// the number of elements in the sorted list after using clear() function is displayed
Console.WriteLine("The count of the elements in the sorted list after using the clear() function is : "
+ List.Count);
// the capacity of the sorted list after using the clear() function is displayed
Console.WriteLine("The sorted list's capacity after using the clear() function is : "
+ List.Capacity);
}
}
Output:
Explanation: In the above program, a class called program is created. Then the main method is called. Then a new sorted list is created. Then elements are added to the newly created sorted list in the form of keys and values pairs. Then the count of the elements of the sorted list are displayed by using count property. Then by using the capacity property of sorted list, the sorted list is checked for its capacity. Then by using the clear property of sorted list, the elements in the sorted list are deleted. Then again the count of the elements of the sorted list are displayed by using count property. Then by using the capacity property of sorted list again, the sorted list is checked for its capacity. The output of the program is shown in the snapshot above.
Advantages
There are several advantages of using SortedList in C#. They are:
- SortedList does not allow duplicate keys.
- Values of the same types and values of different types can be stored in a SortedList due to non-generic collection.
- The key and value pairs of the SortedList can be cast into Dictionary entry.
- IEnumerable, Icollection, Iclonable, and Dictionary interfaces are implemented by the SortedList class.
Conclusion: In this tutorial, we understand the concept of SortedList in C# through definition, the syntax of SortedList in C#, working of SortedList in C# through examples and their outputs and advantages of using SortedList in C#.
Recommended Article
This is a guide to C# SortedList. Here we discuss the Introduction to C# SortedList and its advantages along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –