C# | How to create a SortedList Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. Properties of SortedList: Internally the object of SortedList maintains two arrays. The first array is used to store the elements of the list i.e. keys and the second one is used to store the associated values. A key cannot be null but value can be. As SortedList used sorting which makes it slower in comparison to Hashtable. The capacity of a SortedList can be dynamically increased through reallocation. The keys in the SortedList cannot be duplicated but values can be. The SortedList can be sorted according to the keys using the IComparer(Either in ascending or descending order). Below programs illustrate how to create a SortedList: Example 1: CSharp // C# Program to illustrate how // to create a SortedList using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating object of SortedList // fslist is the SortedList object SortedList fslist = new SortedList(); // Count property is used to get the // number of key/value pairs in fslist // It will give 0 as no pairs are present Console.WriteLine(fslist.Count); } } Output: 0 Example 2: CSharp // C# Program to illustrate how // to create a SortedList using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating object of SortedList // fslist is the SortedList object SortedList fslist = new SortedList(); // Count property is used to get the // number of key/value pairs in fslist // It will give 0 as no pairs are present Console.WriteLine(fslist.Count); // Adding key/value pairs in fslist fslist.Add("1", "GFG"); fslist.Add("2", "Geeks"); fslist.Add("3", "for"); fslist.Add("4", "Geeks"); // Count property is used to get the // number of key/value pairs in fslist // It will give output 4 Console.WriteLine(fslist.Count); } } Output: 0 4 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# SortedList Class K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | How to create a SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. Properties : In C#, SortedSet class can be used to store, remove or view elements. It maintains ascending order and does not store duplicate elements. It is suggeste 2 min read C# | Add element to SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.Add(T) Method is used to add an element to the set and returns a value that specify if it was successfully added or not. Properties: In C#, SortedSet class 2 min read C# | Add element to SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.Add(T) Method is used to add an element to the set and returns a value that specify if it was successfully added or not. Properties: In C#, SortedSet class 2 min read C# SortedList Class SortedList class in C# is a collection of key-value pairs that are sorted by keys. By default, it sorts the key-value pairs in ascending order. It is both a generic and non-generic type, of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic 6 min read C# SortedList Class SortedList class in C# is a collection of key-value pairs that are sorted by keys. By default, it sorts the key-value pairs in ascending order. It is both a generic and non-generic type, of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic 6 min read C# SortedList In C#, SortedList is a collection of key-value pairs sorted according to keys. By default, this collection sorts ascendingly It is of both generic and non-generic type of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic SortedList is defin 7 min read Like