C# | How to add key/value pairs in 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. SortedList.Add(Object, Object) Method is used to add an element with the specified key and value to a SortedList object. Properties of SortedList: Internally the object of SortedList maintain the two arrays. 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 compare 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). Syntax: public virtual void Add (object key, object value); Parameters: key: It is the key of the element which is to be added. value: It is the value of the element which is to be added. The value can be null. Exceptions: ArgumentNullException: If the key is null. ArgumentException: If the element with the specified key is already exists in the SortedList object or SortedList is set to use the IComparable interface and the key does not implement the IComparable interface. NotSupportedException: If the SortedList is read-only or has a fixed size. OutOfMemoryException: If there is not enough available memory in the system to add the pairs to the SortedList. InvalidOperationException: If the comparer throws an exception. Below programs illustrate the use of above discussed method: Example 1: CSharp // C# program to illustrate how to add key/value // pair in SortedList using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating a sorted list of key/value pairs SortedList fslist = new SortedList(); // Adding pairs to fslist fslist.Add("Maths ", 98); fslist.Add("English ", 99); fslist.Add("Physics ", 97); fslist.Add("Chemistry", 96); fslist.Add("CSE ", 100); // Displays the marks in different // subjects sorted according to keys // i.e subjects // Here Count property is used to count // the total number of pairs in SortedList for (int i = 0; i < fslist.Count; i++) { Console.WriteLine("{0}:\t{1}", fslist.GetKey(i), fslist.GetByIndex(i)); } } } Output: Chemistry: 96 CSE : 100 English : 99 Maths : 98 Physics : 97 Example 2: CSharp // C# program to illustrate how to add // key/value pair in SortedList using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating a sorted list of key/value pairs SortedList fslist = new SortedList(); // Adding pairs to fslist fslist.Add("Maths ", 98); fslist.Add("English ", 99); fslist.Add("Physics ", 97); fslist.Add("Chemistry", 96); // this will give error as we are // adding duplicate key i.e Chemistry fslist.Add("Chemistry", 100); } } Error: Unhandled Exception: System.ArgumentException: Item has already been added. Key in dictionary: 'Chemistry' Key being added: 'Chemistry' at System.Collections.SortedList.Add (System.Object key, System.Object value) in :0 at Geeks.Main (System.String[] args) in :0 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.add?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Getting the keys in a SortedList object K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Getting the Values in a SortedList object SortedList.Values Property is used to get the values in a SortedList object. Syntax: public virtual System.Collections.ICollection Values { get; } Property Value: An ICollection object containing the values in the SortedList object. Below programs illustrate the use of above-discussed property: Exam 2 min read C# | Getting the keys in a SortedList object SortedList.Keys Property is used to get the keys in a SortedList object. Syntax: public virtual System.Collections.ICollection Keys { get; } Property Value: An ICollection object containing the keys in the SortedList object. Below programs illustrate the use of above-discussed property: Example 1: C 2 min read C# | Get or set the value associated with specified key in SortedList SortedList.Item[Object] Property is used to get and set the value associated with a specific key in a SortedList object. Syntax: public virtual object this[object key] { get; set; } Here, the key is associated with the value to get or set. It is of the object type. Return Value: This property return 4 min read C# | How to create a SortedList 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 arr 2 min read C# | Getting the list of Values of a SortedList object SortedList.GetValueList Method is used to get the list of keys in a SortedList object. Syntax: public virtual System.Collections.IList GetValueList (); Return Value: It returns an IList object containing the values in the SortedList object. Below programs illustrate the use of above-discussed method 2 min read C# | Search in a SortedList object 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. SortedList.ContainsKey(Object) method is used to check whether a SortedList obj 3 min read Like