Queue Class

Represents a first-in, first-out collection of objects.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public ref class Queue : ICollection, IEnumerable, ICloneable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class Queue implements ICollection, IEnumerable, 
public class Queue implements ICollection, IEnumerable, 
Not applicable.

Queues are useful for storing messages in the order they were received for sequential processing. This class implements a queue as a circular array. Objects stored in a Queue are inserted at one end and removed from the other.

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 through reallocation. The capacity can be decreased by calling TrimToSize.

The growth factor is the number by which the current capacity is multiplied when a greater capacity is required. The growth factor is determined when the Queue is constructed. The default growth factor is 2.0. The capacity of the Queue will always increase by at least a minimum of four, regardless of the growth factor. For example, a Queue with a growth factor of 1.0 will always increase in capacity by four when a greater capacity is required.

Queue accepts a null reference (Nothing in Visual Basic) as a valid value and allows duplicate elements.

For the generic version of this collection, see System.Collections.Generic.Queue

The following example shows how to create and add values to a Queue and how to print out its values.

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection );
int main()
   // Creates and initializes a new Queue.
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue( "Hello" );
   myQ->Enqueue( "World" );
   myQ->Enqueue( "!" );
   // Displays the properties and values of the Queue.
   Console::WriteLine( "myQ" );
   Console::WriteLine( "\tCount:    {0}", myQ->Count );
   Console::Write( "\tValues:" );
   PrintValues( myQ );

void PrintValues( IEnumerable^ myCollection )
   IEnumerator^ myEnum = myCollection->GetEnumerator();
   while ( myEnum->MoveNext() )
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "    {0}", obj );


 This code produces the following output.
     Count:    3
     Values:    Hello    World    !

import System.*;
import System.Collections.*;

public class SamplesQueue
    public static void main(String[] args)
        // Creates and initializes a new Queue.
        Queue myQ = new Queue();

        // Displays the properties and values of the Queue.
            "\tCount:    {0}",System.Convert.ToString(myQ.get_Count()));
    } //main

    public static void PrintValues(IEnumerable myCollection)
        IEnumerator enumerator = myCollection.GetEnumerator();
        while(enumerator.MoveNext()) {
            Object obj = enumerator.get_Current();
            Console.Write("    {0}", obj);
    } //PrintValues
} //SamplesQueue

 This code produces the following output.
     Count:    3
     Values:    Hello    World    !


Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

To guarantee the thread safety of the Queue, all operations must be done through the wrapper returned by the Synchronized method.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions