C# Queue TrimExcess

Description

Queue TrimExcess sets the capacity to the actual number of elements in the Queue , if that number is less than 90 percent of current capacity.

Syntax

Queue.TrimExcess has the following syntax.


public void TrimExcess()

Returns

Queue.TrimExcess method returns

Example


//from  w  w w  .  j av a2 s.  c  o m
using System;
using System.Collections.Generic;

class Example
{
    public static void Main()
    {
        Queue<string> numbers = new Queue<string>();
        numbers.Enqueue("one");
        numbers.Enqueue("two");
        numbers.Enqueue("three");
        numbers.Enqueue("four");
        numbers.Enqueue("five");
        
        numbers.TrimExcess();

        foreach( string number in numbers )
        {
            Console.WriteLine(number);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack