Member-only story
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…