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.
- 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.
public void TrimExcess ();
- 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.
- MathF.Max() Method in C# with Examples
- MathF.Log() Method in C# with Examples
- MathF.Cos() Method in C# with Examples
- MathF.Tan() Method in C# with Examples
- MathF.Exp() Method in C# with Examples
- MathF.Min() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- MathF.Abs() Method in C# with Examples
- MathF.Pow() Method in C# with Examples
- Type.FindMembers() Method in C# with Examples
- UInt64.Equals Method in C# with Examples
- UInt32.ToString Method in C# with Examples | Set - 2
- UInt16.ToString Method in C# with Examples | Set - 2
- Single.CompareTo() Method in C# with Examples
- Single.IsFinite() Method in C# with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.