C# | Remove the element with the specified key from a SortedList Last Updated : 11 Oct, 2018 Comments Improve Suggest changes 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.Remove(Object) method is used to remove the element with the specified key from 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 Remove (object key); Here, key is the key of the element to remove. Exceptions: NotSupportedException : If the SortedList object is read-only or the SortedList has a fixed size. ArgumentNullException : If the key is null. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove the element // with the specified key from 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("IN", "INDIA"); mySortedList.Add("NY", "NEW-YORK"); mySortedList.Add("UK", "UNITED KINGDOM"); mySortedList.Add("GER", "GERMANY"); mySortedList.Add("CH", "CHINA"); // 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 having key as "UK" Console.WriteLine("Removing element having key as UK"); mySortedList.Remove("UK"); // 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 = CH Key = GER Key = IN Key = NY Key = UK Key = CHINA Key = GERMANY Key = INDIA Key = NEW-YORK Key = UNITED KINGDOM Removing element having key as UK Key = CH Key = GER Key = IN Key = NY Key = CHINA Key = GERMANY Key = INDIA Key = NEW-YORK Example 2: CSHARP // C# code to remove the element // with the specified key from a SortedList using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating an SortedList SortedList mySortedList = new SortedList(); // Adding elements to SortedList mySortedList.Add("IN", "INDIA"); mySortedList.Add("NY", "NEW-YORK"); mySortedList.Add("UK", "UNITED KINGDOM"); mySortedList.Add("GER", "GERMANY"); mySortedList.Add("CH", "CHINA"); // 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 having key as null Console.WriteLine("Removing element having key as null"); // It should raise ArgumentNullException // as key can not be null mySortedList.Remove(null); // 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.ArgumentNullException: Key cannot be null. Parameter name: key Note: If the SortedList object does not contain an element with the specified key, the SortedList remains unchanged. No exception is thrown. This method is an O(n) operation, where n is Count. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.remove?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 the element with the specified key from the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Remove(Object) Method is used to remove the element with the specified key from the Hashtable. Syntax: public virt 2 min read C# | Remove from the specified index of 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.RemoveAt(Int32) method is used to remove the element at the specifie 4 min read C# | Removing the specified element from the List List.Remove(T) Method is used to remove the first occurrence of a specific object from the List. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for reference types and it also allows duplicate elem 2 min read C# | Remove all elements from the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Clear Method is used to remove the all elements from the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elements. 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 Like