C# | List.TrimExcess Method Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report List<T>.TrimExcess Method is used to set the capacity to the actual number of elements in the List<T>, if that number is less than a threshold value. Syntax: public void TrimExcess (); Note: This method can be used to minimize a collection’s memory overhead if no new elements will be added to the collection. To reset a List<T> to its initial state, call the Clear method before calling TrimExcess method. Trimming an empty List<T> sets the capacity of the List<T> to the default capacity. The cost of reallocating and copying a large List<T> can be considerable, however, so the TrimExcess method does nothing if the list is at more than 90 percent of capacity. This avoids incurring a large reallocation cost for a relatively small gain. This method is an O(n) operation, where n is Count. Below programs illustrate the use of List<T>.TrimExcess Method: Example 1: CSharp // C# code to set the capacity to the // actual number of elements in the List using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a List of strings List<string> mylist = new List<string>(); // Inserting elements into List mylist.Add("1"); mylist.Add("2"); mylist.Add("3"); mylist.Add("4"); mylist.Add("5"); // To display the capacity of list Console.WriteLine(mylist.Capacity); // To display number of elements in List Console.WriteLine(mylist.Count); // using TrimExcess method mylist.TrimExcess(); // To display the capacity of list Console.WriteLine(mylist.Capacity); // To display number of elements in List Console.WriteLine(mylist.Count); } } Output: 8 5 5 5 Example 2: CSharp // C# code to set the capacity to the // actual number of elements in the List using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a List of integers List<int> mylist = new List<int>(); // Inserting elements into List mylist.Add(45); mylist.Add(78); mylist.Add(32); mylist.Add(231); mylist.Add(123); mylist.Add(76); mylist.Add(726); mylist.Add(716); mylist.Add(876); // To display the capacity of list Console.WriteLine(mylist.Capacity); // To display number of elements in List Console.WriteLine(mylist.Count); // using TrimExcess method mylist.TrimExcess(); // To display the capacity of list Console.WriteLine(mylist.Capacity); // To display number of elements in List Console.WriteLine(mylist.Count); // using clear method mylist.Clear(); // To display the capacity of list Console.WriteLine(mylist.Capacity); // To display number of elements in List Console.WriteLine(mylist.Count); } } Output: 16 9 9 9 9 0 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.trimexcess?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Queue.TrimExcess Method with Examples K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Generic-List CSharp-Generic-Namespace Similar Reads C# | Queue<T>.TrimExcess Method with Examples Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access to items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called Dequeue. Queue<T>.TrimExcess Method is used to set the capacity to the act 2 min read C# | Queue<T>.TrimExcess Method with Examples Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access to items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called Dequeue. Queue<T>.TrimExcess Method is used to set the capacity to the act 2 min read C# | Stack<T>.TrimExcess Method with Examples Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to 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>.TrimExcess Method is used to set the capacity 2 min read C# | Stack<T>.TrimExcess Method with Examples Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to 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>.TrimExcess Method is used to set the capacity 2 min read C# String Trim() Method C# Trim() is a string class method. This method is used to remove all leading and trailing white-space characters from the current String object and return a new modified string. This method is beneficial for clearing the unwanted extra spaces from the string.Example 1: Using the Trim() method to re 4 min read C# String Trim() Method C# Trim() is a string class method. This method is used to remove all leading and trailing white-space characters from the current String object and return a new modified string. This method is beneficial for clearing the unwanted extra spaces from the string.Example 1: Using the Trim() method to re 4 min read Like