C# | Sets the capacity to the actual number of elements in the ArrayList Last Updated : 13 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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 void TrimToSize (); Exception: This method will give NotSupportedException if the ArrayList is read-only or has a fixed size. Below given are some examples to understand the implementation in a better way: Example 1: CSharp // C# program to illustrate the TrimToSize() Method using System; using System.Collections; class GFG { // Main method public static void Main() { // create and initialize new ArrayList ArrayList mylist = new ArrayList(); mylist.Add("Geeks"); mylist.Add("GFG"); mylist.Add("DSA"); mylist.Add("C#"); mylist.Add("Java"); mylist.Add("C++"); // Capacity of ArrayList before trimming Console.WriteLine("Before trimming the capacity is: {0}", mylist.Capacity); // trim the ArrayList mylist.TrimToSize(); // Capacity of ArrayList after trimming Console.WriteLine("After trimming the capacity is: {0}", mylist.Capacity); } } Output: Before trimming the capacity is: 8 After trimming the capacity is: 6 Example 2: CSharp // C# program to illustrate the TrimToSize() Method using System; using System.Collections; class GFG { // Main Method public static void Main() { // Creating and initializing a new ArrayList. ArrayList mylist = new ArrayList(); mylist.Add("C# "); mylist.Add("Java "); mylist.Add("C++ "); mylist.Add("DSA "); mylist.Add("Python "); mylist.Add("Web"); // Displaying the count, capacity // and values of the ArrayList. Console.WriteLine("Before Using TrimToSize Method:"); Console.WriteLine("Count: {0}", mylist.Count); Console.WriteLine("Capacity: {0}", mylist.Capacity); Console.Write("Values are: "); Display(mylist); Console.WriteLine(); // Trim the ArrayList. mylist.TrimToSize(); // Displaying the count, capacity // and values of the ArrayList. Console.WriteLine("After Using TrimToSize Method:"); Console.WriteLine("Count: {0}", mylist.Count); Console.WriteLine("Capacity: {0}", mylist.Capacity); Console.Write("Values are: "); Display(mylist); // Clear the ArrayList. mylist.Clear(); Console.WriteLine(); // Displaying the count, capacity // and values of the ArrayList. Console.WriteLine("After Using Clear Method:"); Console.WriteLine("Count: {0}", mylist.Count); Console.WriteLine("Capacity: {0}", mylist.Capacity); Console.Write("Values are: "); Display(mylist); // again trim the ArrayList mylist.TrimToSize(); Console.WriteLine(); // Displaying the count, capacity // and values of the ArrayList. Console.WriteLine("After Again Using TrimToSize Method:"); Console.WriteLine("Count: {0}", mylist.Count); Console.WriteLine("Capacity: {0}", mylist.Capacity); Console.Write("Values are: "); Display(mylist); } // to display the values of ArrayList public static void Display(IEnumerable ienum) { foreach (Object ob in ienum) Console.Write("{0}", ob); Console.WriteLine(); } } Output: Before Using TrimToSize Method: Count: 6 Capacity: 8 Values are: C# Java C++ DSA Python Web After Using TrimToSize Method: Count: 6 Capacity: 6 Values are: C# Java C++ DSA Python Web After Using Clear Method: Count: 0 Capacity: 6 Values are: After Again Using TrimToSize Method: Count: 0 Capacity: 4 Values are: Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.trimtosize?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Sets the capacity to the actual number of elements in the ArrayList A ankita_saini Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-ArrayList Similar Reads C# | Get the number of elements actually contained in 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.Count property gets the number of elements actually contained in 3 min read C# | Setting the capacity to the actual number of elements in a SortedList object 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 illu 3 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# | Get or set the number of elements in the BitArray The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Length property is used to get or set the number of 2 min read C# | Total number of elements present in an array Array.GetLength(Int32) Method is used to find the total number of elements present in the specified dimension of the Array. Syntax: public int GetLength (int dimension); Here, dimension is a zero-based dimension of the Array whose length needs to be determined.Return value: The return type of this m 2 min read Like