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

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

Syntax

Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class Queue
    Implements ICollection, IEnumerable, ICloneable
Visual Basic (Usage)
Dim instance As Queue
C#
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class Queue : ICollection, IEnumerable, ICloneable
C++
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class Queue : ICollection, IEnumerable, ICloneable
J#
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class Queue implements ICollection, IEnumerable, 
    ICloneable
JScript
SerializableAttribute 
ComVisibleAttribute(true) 
public class Queue implements ICollection, IEnumerable, 
    ICloneable
XAML
Not applicable.
Remarks

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

Example

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

Visual Basic
Imports System
Imports System.Collections

Public Class SamplesQueue

    Public Shared Sub Main()

        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("Hello")
        myQ.Enqueue("World")
        myQ.Enqueue("!")

        ' Displays the properties and values of the Queue.
        Console.WriteLine("myQ")
        Console.WriteLine("    Count:    {0}", myQ.Count)
        Console.Write("    Values:")
        PrintValues(myQ)

    End Sub 'Main

    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myCollection
            Console.Write("    {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

End Class 'SamplesQueue


' This code produces the following output.
' 
' myQ
'     Count:    3
'     Values:    Hello    World    !
C#
 using System;
 using System.Collections;
 public class SamplesQueue  {
 
    public static void Main()  {
 
       // Creates and initializes a new Queue.
       Queue myQ = new 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 );
    }
 
 
    public static void PrintValues( IEnumerable myCollection )  {
       foreach ( Object obj in myCollection )
          Console.Write( "    {0}", obj );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 myQ
     Count:    3
     Values:    Hello    World    !
*/ 
C++
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 );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 myQ
     Count:    3
     Values:    Hello    World    !
*/
J#
import System.*;
import System.Collections.*;

public class SamplesQueue
{
    public static void main(String[] args)
    {
        // Creates and initializes a new Queue.
        Queue myQ = new Queue();
        myQ.Enqueue("Hello");
        myQ.Enqueue("World");
        myQ.Enqueue("!");

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

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

/* 
 This code produces the following output.
 
 myQ
     Count:    3
     Values:    Hello    World    !
 */
Inheritance Hierarchy

System.Object
  System.Collections.Queue
Thread Safety

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.

Platforms

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.

Version Information

.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
See Also

Tags :


Community Content

Joshua Flanagan
Look at Queue<T> instead
System.Collections.Generic.Queue<T> provides a typesafe way to enqueue and dequeue items in a queue. It will generally result in a better developer experience, and can improve performance (it avoids boxing/unboxing of value types).
Tags :

Page view tracker