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.
- 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.
public virtual void Remove (object key);
Here, key is the key of the element to remove.
- 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:
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
System.ArgumentNullException: Key cannot be null.
Parameter name: key
- 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.
- C# | Get or set the value associated with specified key in SortedList
- C# | Getting the key at the specified index of a SortedList object
- C# | Getting the index of the specified key in a SortedList object
- C# | Remove from the specified index of a SortedList
- C# | Getting the value at the specified index of a SortedList object
- C# | Getting index of the specified value in a SortedList object
- C# | Remove the element with the specified key from the Hashtable
- C# | How to add key/value pairs in SortedList
- C# | Check whether a SortedList object contains a specific key
- C# | Remove entry with specified key from the StringDictionary
- C# | Remove the entry with specified key from ListDictionary
- C# | Remove entry with specified key from OrderedDictionary
- C# | Remove all elements from a SortedList
- C# | Remove the specified element from a HashSet
- C# | Remove the element at the specified index of the ArrayList
- C# | How to remove the element from the specified index of the List
- C# | Remove element at specified index of Collection<T>
- C# | Get or Set the value associated with specified key in Hashtable
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | Add the specified key and value into the ListDictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.