Member-only story
SortedList in C# with Examples
3 min readApr 18, 2024
The SortedList is related to System.Collections namespace. SortedList contains on key-value pairs. Where the keys are automatically sorted in ascending order. It is similar to a SortedList and is used when you need to maintain a collection of key-value pairs sorted by the keys.
Features with sortedlist in C#:
Creating a SortedList:
To work with a SortedList, you can create one like this:
Example
using System;
using System.Collections.Generic;
LinkedList<int> linkedfruitprices = new LinkedList<int>();
LinkedList<string> linkedfruitnames = new LinkedList<string>();
Adding Key-Value Pairs:
You can add key-value pairs to a SortedList using the Add method:
Example
dictfruitprices.Add("Apple",16);
dictfruitprices.Add("Orange",12);
dictfruitprices.Add("Cherry",18);
dictfruitprices["Papaya"] = 20;
dictfruitprices["Grape"] = 6;
dictfruitnames.Add("Apple","Red");
dictfruitnames.Add("Mango","Yellow");
dictfruitnames.Add("Cherry","Red");
dictfruitnames.Add("Orange","Dark Orange");
dictfruitnames["Grape"] = "Violet";
dictfruitnames.["Kiwi"] = "Spring Green";