C# | Remove all elements from 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.Clear method is used to remove all the elements 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 Clear (); Exceptions: NotSupportedException : If the SortedList object is read-only or has a fixed size. Example: CSHARP // C# code to remove all // elements 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("1", "1st"); mySortedList.Add("2", "2nd"); mySortedList.Add("3", "3rd"); mySortedList.Add("4", "4th"); mySortedList.Add("5", "5th"); mySortedList.Add("6", "6th"); mySortedList.Add("7", "7th"); // Displaying number of elements Console.WriteLine("Number of elements in SortedList is : " + mySortedList.Count); // Displaying capacity Console.WriteLine("capacity of SortedList is : " + mySortedList.Capacity); // Removing all elements from SortedList mySortedList.Clear(); // Displaying number of elements Console.WriteLine("Number of elements in SortedList is : " + mySortedList.Count); // Displaying capacity Console.WriteLine("capacity of SortedList is : " + mySortedList.Capacity); } } Output: Number of elements in SortedList is : 7 capacity of SortedList is : 16 Number of elements in SortedList is : 0 capacity of SortedList is : 16 Note: This method is an O(n) operation, where n is Count. Count is set to zero and references to other objects from elements of the collection are also released. Capacity remains unchanged. To reset the capacity of the SortedList object, call TrimToSize or set the Capacity property directly. Trimming an empty SortedList sets the capacity of the SortedList to the default capacity. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.clear?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Remove all elements from the ArrayList S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads 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# | 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# | Remove all elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Clear method is used to remove all the elements from the ArrayLis 3 min read C# | Remove all elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Clear method is used to remove all the elements from the ArrayLis 3 min read C# | Removing all the elements from the List List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. L 3 min read C# | Removing all the elements from the List List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. L 3 min read Like