C# | Setting the capacity to the actual number of elements in a SortedList object Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report SortedList.TrimToSize Method is used to set the capacity to the actual number of elements in a SortedList object.Syntax: public virtual void TrimToSize (); Exception: This method will throw NotSupportedException if the SortedList object is read-only or SortedList has a fixed size.Below programs illustrate the use of above-discussed method:Example 1: CSharp // C# program to illustrate the // SortedList.TrimToSize() Method using System; using System.Collections; class GFG { // Main method public static void Main() { // create and initialize new SortedList SortedList mylist = new SortedList(); // Adding elements to SortedList mylist.Add("1", "C++"); mylist.Add("2", "Java"); mylist.Add("3", "DSA"); mylist.Add("4", "Python"); mylist.Add("5", "C#"); mylist.Add("6", "HTML"); // Capacity of SortedList before trimmimg Console.WriteLine("Before trimming the capacity is: {0}", mylist.Capacity); // trim the SortedList mylist.TrimToSize(); // Capacity of ArrayList after trimming Console.WriteLine("After trimming the capacity is: {0}", mylist.Capacity); } } Output: Before trimming the capacity is: 16 After trimming the capacity is: 6 Example 2: CSharp // C# program to illustrate // SortedList.TrimToSize() Method using System; using System.Collections; class GFG { // Main Method public static void Main() { // create and initialize new SortedList SortedList mylist = new SortedList(); // Adding elements to SortedList mylist.Add("h", "Hello"); mylist.Add("g", "Geeks!"); mylist.Add("w", "Welcome"); mylist.Add("t", "to"); mylist.Add("n", "Noida"); // Displaying the count, capacity // and values of the SortedList. Console.WriteLine("Before Using TrimToSize Method:"); Console.WriteLine(); Console.WriteLine("Count: {0}", mylist.Count); Console.WriteLine("Capacity: {0}", mylist.Capacity); Console.Write("Values are: "); Display(mylist); Console.WriteLine(); // Trim the SortedList. mylist.TrimToSize(); // Displaying the count, capacity // and values of the SortedList. Console.WriteLine(); Console.WriteLine("After Using TrimToSize Method:"); Console.WriteLine(); Console.WriteLine("Count: {0}", mylist.Count); Console.WriteLine("Capacity: {0}", mylist.Capacity); Console.Write("Values are: "); Display(mylist); // Clear the SortedList mylist.Clear(); Console.WriteLine(); // Displaying the count, capacity // and values of the SortedList. Console.WriteLine(); Console.WriteLine("After Using Clear Method:"); Console.WriteLine(); Console.WriteLine("Count: {0}", mylist.Count); Console.WriteLine("Capacity: {0}", mylist.Capacity); Console.Write("Values are: "); Display(mylist); // again trim the SortedList mylist.TrimToSize(); Console.WriteLine(); // Displaying the count, capacity // and values of the SortedList. Console.WriteLine(); Console.WriteLine("After Again Using TrimToSize Method:"); Console.WriteLine(); Console.WriteLine("Count: {0}", mylist.Count); Console.WriteLine("Capacity: {0}", mylist.Capacity); Console.Write("Values are: "); Display(mylist); } // to display the values of SortedList public static void Display(SortedList mylist) { // taking an IList and // using GetValueList method IList vlist = mylist.GetValueList(); for (int i = 0; i < mylist.Count; i++) Console.Write(vlist[i] + " "); } } Output: Before Using TrimToSize Method: Count: 5 Capacity: 16 Values are: Geeks! Hello Noida to Welcome After Using TrimToSize Method: Count: 5 Capacity: 5 Values are: Geeks! Hello Noida to Welcome After Using Clear Method: Count: 0 Capacity: 5 Values are: After Again Using TrimToSize Method: Count: 0 Capacity: 0 Values are: Note: The main use of this method is that it can be used to minimize a collection's memory overhead if no new elements will be added to the collection.To reset a SortedList object to its initial state, call the Clear method before calling TrimToSize. Trimming an empty SortedList set the capacity of the SortedList to the default capacity.This method is an O(n) operation, where n is Count. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.trimtosize?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Get the number of elements in the SortedSet K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Sets the capacity to the actual number of elements in the ArrayList ArrayList.TrimToSize Method is used to set the capacity to the actual number of elements in the ArrayList. It can be used to minimize a collection's memory overhead if no new elements will be added to the collection. Note: This method is an O(n) operation, where n is Count. Syntax: public virtual vo 3 min read C# | Copying the SortedList elements to an Array Object SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destinat 2 min read C# | Get the number of elements contained in SortedList SortedList.Count Property is used to get the number of elements contained in a SortedList object. Syntax: public virtual int Count { get; } Property Value: The number of elements contained in the SortedList object. Below programs illustrate the use of above discussed method: Example 1: CSharp // C# 2 min read C# | Get the number of elements in the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Count Property is used to get the number of elements in the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elemen 2 min read C# | Get or set the number of elements that the ArrayList can contain 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.Capacity property is used to get or set the number of elements th 2 min read C# | Insert an object at the top of the Stack - Push Operation Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.Push(T) Method is used to inserts an object 2 min read Like