Introduction
C# SortedList<> class represents a collection of key/value pairs that are sorted by the key. C# SortedList is a generic collection, defined as SortedList<TKey, TValue>, where Tkey is the type of keys in the collection and Tvalue is the type of values in the collection. In this article, I explain how to create a SortedList object and use SortedList methods and SortedList properties on the collection.
Create SortedList
C# SortedList class is defined in the System.Collections namespace. Make sure to import the namespace in your code before using it.
We can create a SortedList object as following:
- SortedList<int, string> sortedlist = new SortedList<int, string>();
Here, the key is an int type and the value is a string type. You can create whatever type you like.
Add Items to SortedList
SortedList.Add() method adds items to the collection. The following code snippet adds several items to the colleciton.
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
-
- Console.WriteLine("The elements in the SortedList are:");
- foreach(KeyValuePair < int, string > pair in sortedlist) {
- Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
- }
- }
- }
- }
The above code also reads the SortedList collection items using a foreach loop and read using a KeyValuePair object. The read items are displayed on the console.
The output looks like the following:
SortedList Properties
Let's look at some of the common properties of SortedList.
SortedList.Capacity
The Capacity property gets or sets the number of elements that the SortedList<TKey, TValue> can contain. The default value of capacity is 4, which allows 4 elements to be added to the list, and if we add a 5th element to the collection, the capacity increases by 4 more, which means if there are 5 elements in the list then the capacity is 8.
In the following example, we add an element with the number 7, so is the capacity is 8; if we add 9th elements, the capacity will be 16 (double the previous one).
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
-
- Console.WriteLine("The elements in the SortedList are:");
- foreach(KeyValuePair < int, string > pair in sortedlist) {
- Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
- }
-
- Console.WriteLine("The capacity is:" + sortedlist.Capacity);
- sortedlist.Add(8, "januray");
- sortedlist.Add(9, "april");
- Console.WriteLine("After adding two more element the capacity is:" + sortedlist.Capacity);
- }
- }
- }
The output looks like the following:
SortedList.Count
The Count property counts the number of elements in the SortedList or we can say that Count gets the number of key/value pairs contained in the SortedList<TKey, TValue>. For example:
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
-
- Console.WriteLine("The elements in the SortedList are:");
- foreach(KeyValuePair < int, string > pair in sortedlist) {
- Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
- }
- Console.WriteLine("The total number of elements in the sortedlist are:" + sortedlist.Count);
- sortedlist.Add(8, "januray");
- sortedlist.Add(9, "april");
- Console.WriteLine("After adding two more element the number of element in sortedlist are:" + sortedlist.Count);
- }
- }
- }
The output looks like the following:
Difference between Capacity and Count
SortedList.Keys
The Keys property gets a collection containing the keys in the SortedList<TKey, TValue>; for this we use IList, for example:
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
-
- Console.WriteLine("The elements in the SortedList are:");
- foreach(KeyValuePair < int, string > pair in sortedlist) {
- Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
- }
- IList < int > ilistKeys = sortedlist.Keys;
- Console.WriteLine();
- Console.WriteLine("The keys are:");
- Console.Write("{");
- foreach(int i in ilistKeys) {
- Console.Write(i + ",");
- }
- Console.WriteLine("}");
- }
- }
- }
The output looks like the following:
SortedList.Values
The Values property gets a collection containing the values in the SortedList<TKey, TValue>. For example:
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
-
- Console.WriteLine("The elements in the SortedList are:");
- foreach(KeyValuePair < int, string > pair in sortedlist) {
- Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
- }
- IList < string > ilistvalues = sortedlist.Values;
- Console.WriteLine();
- Console.WriteLine("The Values are:");
- Console.WriteLine();
- Console.Write("{");
- foreach(string i in ilistvalues) {
- Console.Write(i + ",");
- }
- Console.WriteLine("}");
- }
- }
- }
The output looks like the following:
SortedList.Methods
Here is a list of some of the useful SortedList class methods.
SortedList.ContainsKey()
This method determines whether the SortedList<TKey, TValue> contains a specific key. For example:
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
-
- Console.WriteLine("The elements in the SortedList are:");
- foreach(KeyValuePair < int, string > pair in sortedlist) {
- Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
- }
-
- Console.WriteLine("The key 1 contain in the SortedList:" + sortedlist.ContainsKey(1));
- Console.WriteLine("The key 5 contain in the SortedList:" + sortedlist.ContainsKey(5));
- Console.WriteLine("The key 10 contain in the SortedList:" + sortedlist.ContainsKey(10));
- Console.WriteLine("The key 50 contain in the SortedList:" + sortedlist.ContainsKey(50));
- }
- }
- }
The output looks like the following:
SortedList.ContainsValue()
This method determines whether the SortedList<TKey, TValue> contains a specific value. For example:
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
-
- Console.WriteLine("The elements in the SortedList are:");
- foreach(KeyValuePair < int, string > pair in sortedlist) {
- Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
- }
-
- Console.WriteLine("The value Sunday contain in the SortedList:" + sortedlist.ContainsValue("Sunday"));
- Console.WriteLine("The value Friday contain in the SortedList:" + sortedlist.ContainsValue("Friday"));
- Console.WriteLine("The value May contain in the SortedList:" + sortedlist.ContainsValue("May"));
- Console.WriteLine("The value Wednesday contain in the SortedList:" + sortedlist.ContainsValue("Wednesday"));
- }
- }
- }
The output looks like the following:
SortedList.Clear()
This method removes all elements from the SortedList<TKey, TValue>. For example:
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
- Console.WriteLine("The number of elements in the sortedlist are:" + sortedlist.Count);
-
- sortedlist.Clear();
- Console.WriteLine("After clear method the elements in the sortedlist are:" + sortedlist.Count);
- }
- }
- }
The output looks like the following:
SortedList.IndexOfKey()
This method searches the specified key and returns the index value of that key.
SortedList.IndexOfValue()
This method searches the specified value and returns the index value of that value.
The following example shows the use of the both IndexOfKey and IndexOfValue methods
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
-
- Console.WriteLine("***************INDEXOFKEY***************");
- Console.WriteLine();
- Console.WriteLine("The index value of the key 4 is:" + sortedlist.IndexOfKey(4));
- Console.WriteLine("The index value of the key 1 is:" + sortedlist.IndexOfKey(1));
- Console.WriteLine("The index value of the key 7 is:" + sortedlist.IndexOfKey(7));
- Console.WriteLine("The index value of the key 2 is:" + sortedlist.IndexOfKey(2));
-
- Console.WriteLine();
- Console.WriteLine("***************INDEXOFVALUE***************");
- Console.WriteLine();
- Console.WriteLine("The index value of the value Sunday is:" + sortedlist.IndexOfValue("Sunday"));
- Console.WriteLine("The index value of the value Wednesday is:" + sortedlist.IndexOfValue("Wednesday"));
- Console.WriteLine("The index value of the value Monday is:" + sortedlist.IndexOfValue("Monday"));
- Console.WriteLine("The index value of the value Friday is:" + sortedlist.IndexOfValue("Friday"));
- }
- }
- }
The output looks like the following:
SortedList.Remove()
This method removes the element with the specified key from the SortedList<TKey, TValue>. For example:
- namespace SortedList {
- class Program {
- static void Main(string[] args) {
-
- SortedList < int, string > sortedlist = new SortedList < int, string > ();
-
- sortedlist.Add(1, "Sunday");
- sortedlist.Add(2, "Monday");
- sortedlist.Add(3, "Tuesday");
- sortedlist.Add(4, "Wednesday");
- sortedlist.Add(5, "Thusday");
- sortedlist.Add(6, "Friday");
- sortedlist.Add(7, "Saturday");
-
- Console.WriteLine("The elements in the SortedList are:");
- foreach(KeyValuePair < int, string > pair in sortedlist) {
- Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
- }
-
- sortedlist.Remove(3);
- sortedlist.Remove(7);
- sortedlist.Remove(1);
- Console.WriteLine("After remove some elements the sortedlist is as:");
- foreach(KeyValuePair < int, string > pair1 in sortedlist) {
- Console.WriteLine("{0} => {1}", pair1.Key, pair1.Value);
- }
- }
- }
- }
The output looks like the following:
Summary
In this tutorial, I've explained how to create a SortedList in C# and use its methods and properties.
Note
If you're new to collections in C#, here is a must read article:
Collections in C#