C# Programming Language: SortedList
Hi everyone, welcome back. In this tutorial, we will be going over SortedLists in C#. SortedLists can be used to store a collection of data, similar to that of a normal list. The main difference is that the SortedLists stores elements as key/value pairs as an alternative to using index numbers. The keys must be unique and cannot be null. The values can be duplicates or be null. With this introduction out of the way, let’s get into it.

Creating SortedList
Let’s start by creating a SortedList. Before we can create the SortedList, we need to make sure that we have our SortedList import, which is included within the System.Collections.Generic package:
using System.Collections.Generic;
Now, we can successfully create our SortedList:
SortedList<int, string> mySL = new SortedList<int, string>();
So now, we have created our SortedList variable. Our keys are of the ‘int’ data type and our values are of the ‘string’ data type. When we create our SortedList, we will need to specify the data types for our keys and values. We can use other data types as well for both.
Adding Elements Into The SortedList
Now, let’s see how we can add elements into the SortedList. We can do this by using the built in Add() function that is provided. Let’s see an example:
SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
foreach(var element in mySL)
{
Console.WriteLine(element.Key + ": " + element.Value);
}Output:
1: One
4: Four
17: Seventeen
In the example above, we can see that we added a few elements into our SortedList by using the Add() function. We can verify that they were successfully added by printing our SortedList and checking the output.
Retrieving the Keys of a SortedList
Now, let’s see how we can retrieve the keys of a SortedList. We can do this by using the built in ‘Keys’ property that is provided. Let’s see an example:
SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
List<int> myKeys = new List<int>(mySL.Keys);
Console.WriteLine(String.Join(" ", myKeys));Output:
1 4 17
In the example above, we can see that we have successfully retrieved the list of keys within our SortedList. We can verify that we retrieved the keys by printing the resulting list and checking the output.
Retrieving the Values of a SortedList
Now, let’s see how we can retrieve the values of a SortedList. We can do this by using the built in ‘Values’ property that is provided. Let’s see an example:
SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
List<string> myValues = new List<string>(mySL.Values);
Console.WriteLine(String.Join(" ", myValues));Output:
One Four Seventeen
In the example above, we can see that we have successfully retrieved the list of values within our SortedList. We can verify that we retrieved the values by printing the resulting list and checking the output.
Checking the Size of The SortedList
Now, let’s see how we can check the size of the SortedList, or how many elements are in the SortedList. We can do this by using the built in ‘Count’ property that is provided. Let’s see an example:
SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
Console.WriteLine(mySL.Count);Output:
3
In the example above, we can see that we added a few elements into our SortedList. We then printed the result of the count property on our SortedList and we can check our output to see how many elements we have.
Checking if The SortedList Contains a Key
Now, let’s try to see if our SortedList contains a specific key. We can do this by using the built in ContainsKey() function that is provided. Let’s see an example:
SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
Console.WriteLine(mySL.ContainsKey(1));
Console.WriteLine(mySL.ContainsKey(2));Output:
True
False
In the example above, we can see that we added a few elements into our SortedList. Then we printed the results of our ContainsKey() functions. We checked to see if our SortedList contains the keys of ‘1’ and ‘2.’ As we can see in the output, our SortedList contains a key of ‘1,’ but not ‘2.’
Checking if The SortedList Contains a Value
Now, let’s try to see if our SortedList contains a specific value. We can do this by using the built in ContainsValue() function that is provided. Let’s see an example:
SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
Console.WriteLine(mySL.ContainsValue("One"));
Console.WriteLine(mySL.ContainsValue("Two"));Output:
True
False
In the example above, we can see that we added a few elements into our SortedList. Then we printed the results of our ContainsValue() functions. We checked to see if our SortedList contains the values of ‘One’ and ‘Two.’ As we can see in the output, our SortedList contains a value of ‘One,’ but not ‘Two.’
Removing An Element From a SortedList
Now, let’s see how we can remove an element from a SortedList. We can do this by using the built in Remove() function that is provided. Let’s see an example:
SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
mySL.Remove(1);
foreach(var element in mySL)
{
Console.WriteLine(element.Key + ": " + element.Value);
}Output:
4: Four
17: Seventeen
In the example above, we can see that we added a few elements into our SortedList. Then we used the Remove() function to remove the element with the key of ‘1’ from our SortedList. We can verify that we successfully removed the corresponding element by printing our SortedList and checking the output.
Clearing out a SortedList
Now, let’s see how we can completely clear out the SortedList. We can do this by using the built in Clear() function that is provided. Let’s see an example:
SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
mySL.Clear();
foreach(var element in mySL)
{
Console.WriteLine(element.Key + ": " + element.Value);\
}Output:
In the example above, we have added a few elements into our SortedList. Then we used the Clear() function on our SortedList. We can verify that all elements were removed by printing our SortedList and checking the output. Our output should be empty because we removed everything.
Conclusion
That is it for the C# SortedList examples. We covered how to create a SortedList along with some of the functions regarding them. I hope this helps. If there are any questions or comments, please let me know. Thanks for reading.