Set the capacity to the actual number of elements in the Queue in CSharp

Description

The following code shows how to set the capacity to the actual number of elements in the Queue.

Example


/*ww w.ja va 2 s.c om*/
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");
        
        Console.WriteLine(numbers.Count);
        
        numbers.TrimExcess();

        Console.WriteLine(numbers.Count);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary