C# | Queue<T>.TrimExcess Method with Examples Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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 actual number of elements in the Queue<T>, if that number is less than 90 percent of current capacity. A queue is an unbounded data structure and there is no method available to calculate the capacity of Queue in C#. It is dynamic and depends on the system memory. This method is generally used in memory management of the large queue. Queue Properties: Enqueue adds an element to the end of the Queue. Dequeue removes the oldest element from the start of the Queue. Peek returns the oldest element that is at the start of the Queue but does not remove it from the Queue. The capacity of a Queue is the number of elements the Queue can hold. As elements are added to a Queue, the capacity is automatically increased as required by reallocating the internal array. Queue accepts null as a valid value for reference types 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 Queue<T> to its initial state, call the Clear method before calling the TrimExcess method. Trimming an empty Queue<T> sets the capacity of the Queue<T> to the default capacity. Below given is an example to understand the implementation in a better way: Example: CSHARP // C# code to set the capacity to the // actual number of elements in the Queue using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a Queue of strings Queue<string> myQueue = new Queue<string>(); // Inserting elements into Queue myQueue.Enqueue("1st"); myQueue.Enqueue("2nd"); myQueue.Enqueue("3rd"); myQueue.Enqueue("4th"); myQueue.Enqueue("5th"); // To display number of elements in Queue Console.WriteLine(myQueue.Count); // Removing all the elements from Queue myQueue.Clear(); // using TrimExcess method myQueue.TrimExcess(); // To display number of elements in Queue Console.WriteLine(myQueue.Count); } } Output: 5 0 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.queue-1.trimexcess?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Queue<T>.TrimExcess Method with Examples S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Generic-Queue CSharp-Generic-Namespace Similar Reads 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# | 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# Queue with Examples A Queue in C# is a collection that follows the First-In-First-Out (FIFO) principle which means elements are processed in the same order they are added. It is a part of the System.Collections namespace for non-generic queues and System.Collections.Generic namespace for generic queues.Key Features:FIF 6 min read C# | Get the number of elements contained in the Queue Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.Count Property is used to get the number of elements contained i 2 min read Queue.Clear Method in C# This method is used to remove the objects from the Queue. This method is an O(n) operation, where n is the total count of elements. And this method comes under System.Collections namespace. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: 2 min read Like