C# | Stack<T>.TrimExcess Method with Examples Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 to the actual number of elements in the Queue<T>, if that number is less than 90 percent of current capacity. A stack is an unbounded data structure and there is no method available to calculate the capacity of Stack in C#. It is dynamic and depends on the system memory. This method is generally used in memory management of the large Stack. Stack Properties: The capacity of a Stack is the number of elements the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required through reallocation. If Count is less than the capacity of the stack, Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count. Pop is an O(1) operation. Stack accepts null as a valid value and allows duplicate elements. 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 Stack<T> to its initial state, call the Clear method before calling TrimExcess method. Trimming an empty Stack<T> sets the capacity of the Stack<T> to the default capacity. Example: CSHARP // C# code to set the capacity to the // actual number of elements in the Stack using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a Stack of strings Stack<string> myStack = new Stack<string>(); // Inserting elements into Stack myStack.Push("1st"); myStack.Push("2nd"); myStack.Push("3rd"); myStack.Push("4th"); myStack.Push("5th"); // To display number of elements in Stack Console.WriteLine(myStack.Count); // removing all the elements from the stack myStack.Clear(); // using TrimExcess method myStack.TrimExcess(); // To display number of elements in Stack Console.WriteLine(myStack.Count); } } Output: 5 0 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1.trimexcess?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Queue.TrimExcess Method with Examples S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Generic-Stack 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 Stack.ToString() Method in C# with examples ToString method is inherited from the Object class which is used to get a string that represents the current object. It can also apply on the Stack. It returns a string which represents the current stack object. Syntax: public virtual string ToString (); Return Value: This method returns a String re 2 min read String.Format() Method in C# with Examples | Set - 1 In C#, Format() is a method of the String Class. This method is used to replace one or more format items in the specified string with the string representation of a specified object. In other words, this method is used to insert the value of the variable or an object or expression into another strin 4 min read MathF.Truncate() Method in C# with Examples In C#, MathF.Truncate(Single) is a MathF class method which is used to compute an integral part of a specified single number or single-precision floating-point number.Syntax: public static float Truncate (float x); Parameter: x: It is the specified number which is to be truncated and type of this pa 1 min read C# | List.TrimExcess Method 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 add 2 min read C# Stack with Examples In C# a Stack is a Collection that follows the Last-In-First-Out (LIFO) principle. It is used when we need last-in, first-out access to items. In C# there are both generic and non-generic types of Stacks. The generic stack is in the System.Collections.Generic namespace, while the non-generic stack i 6 min read Like