C# | Remove from the specified index of 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. SortedList.RemoveAt(Int32) method is used to remove the element at the specified index of a SortedList object. Properties: A SortedList element can be accessed by its key or by its index. A SortedList object internally maintains two arrays to store the elements of the list, i.e, one array for the keys and another array for the associated values. A key cannot be null, but a value can be. The capacity of a SortedList object is the number of elements the SortedList can hold. A SortedList does not allow duplicate keys. Operations on a SortedList object tend to be slower than operations on a Hashtable object because of the sorting. Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based. Syntax: public virtual void RemoveAt (int index); Here, index is the zero-based index of the element to remove. Exceptions: NotSupportedException : If the SortedList object is read-only or the SortedList has a fixed size. ArgumentOutOfRangeException : If the index is outside the range of valid indexes for the SortedList object. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove the element at // the specified index of a SortedList using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating an SortedList SortedList mySortedList = new SortedList(); // Adding elements to SortedList mySortedList.Add("DS", "Data Structures"); mySortedList.Add("EE", "Electrical Engineering"); mySortedList.Add("CS", "Computer Science"); mySortedList.Add("ME", "Mechanical Engineering"); mySortedList.Add("CE", "Civil Engineering"); // Displaying elements in SortedList foreach(string mykey in mySortedList.Keys) Console.WriteLine("Key = " + mykey); foreach(string myvalue in mySortedList.Values) Console.WriteLine("Key = " + myvalue); // Removing element at index 4 Console.WriteLine("Removing element at index 4"); mySortedList.RemoveAt(4); // Displaying elements in SortedList foreach(string mykey in mySortedList.Keys) Console.WriteLine("Key = " + mykey); foreach(string myvalue in mySortedList.Values) Console.WriteLine("Key = " + myvalue); } } Output: Key = CE Key = CS Key = DS Key = EE Key = ME Key = Civil Engineering Key = Computer Science Key = Data Structures Key = Electrical Engineering Key = Mechanical Engineering Removing element at index 4 Key = CE Key = CS Key = DS Key = EE Key = Civil Engineering Key = Computer Science Key = Data Structures Key = Electrical Engineering Example 2: CSHARP // C# code to remove the element at // the specified index of a SortedList using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating an SortedList SortedList mySortedList = new SortedList(); // Adding elements to SortedList mySortedList.Add("DS", "Data Structures"); mySortedList.Add("EE", "Electrical Engineering"); mySortedList.Add("CS", "Computer Science"); mySortedList.Add("ME", "Mechanical Engineering"); mySortedList.Add("CE", "Civil Engineering"); // Displaying elements in SortedList foreach(string mykey in mySortedList.Keys) Console.WriteLine("Key = " + mykey); foreach(string myvalue in mySortedList.Values) Console.WriteLine("Key = " + myvalue); // Removing element at index 8 Console.WriteLine("Removing element at index 8"); // It should raise ArgumentOutOfRangeException // As index is outside the range of valid // indexes for the SortedList object. mySortedList.RemoveAt(8); // Displaying elements in SortedList foreach(string mykey in mySortedList.Keys) Console.WriteLine("Key = " + mykey); foreach(string myvalue in mySortedList.Values) Console.WriteLine("Key = " + myvalue); } } Error: Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Note: The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element is removed, the indexing also adjusts accordingly. Therefore, the index of a specific key/value pair might change as elements are added or removed from the SortedList object. This method is an O(n) operation, where n is Count. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.removeat?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Remove the element with the specified key from a SortedList S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Remove a specified item from SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Remove(T) Method is used to remove a specified item from the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view eleme 2 min read C# | Remove the element with the specified key from 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. SortedList.Remove(Object) method is used to remove the element with the specifi 3 min read C# | Remove the element with the specified key from 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. SortedList.Remove(Object) method is used to remove the element with the specifi 3 min read C# | Getting index of the specified value in a SortedList object SortedList.IndexOfValue(Object) Method is used to get the zero-based index of the first occurrence of the specified value in a SortedList object. Syntax: public virtual int IndexOfValue (object value); Here, value is the Value which is to be located in the SortedList object. The value can be null. R 3 min read C# | Getting index of the specified value in a SortedList object SortedList.IndexOfValue(Object) Method is used to get the zero-based index of the first occurrence of the specified value in a SortedList object. Syntax: public virtual int IndexOfValue (object value); Here, value is the Value which is to be located in the SortedList object. The value can be null. R 3 min read C# | Remove the entry at specified index from OrderedDictionary OrderedDictionary.RemoveAt(Int32) method is used to remove the entry at the specified index from the OrderedDictionary collection. Syntax: public void RemoveAt (int index); Here, index is the zero-based index of the entry to remove. Exceptions: NotSupportedException : If the OrderedDictionary collec 3 min read Like