Export (0) Print
Expand All

Queue Class

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

For a list of all members of this type, see Queue Members.

System.Object
   System.Collections.Queue

[Visual Basic]
<Serializable>
Public Class Queue
   Implements ICollection, IEnumerable, ICloneable
[C#]
[Serializable]
public class Queue : ICollection, IEnumerable, ICloneable
[C++]
[Serializable]
public __gc class Queue : public ICollection, IEnumerable,
   ICloneable
[JScript]
public
   Serializable
class Queue implements ICollection, IEnumerable, ICloneable

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. 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 could 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.

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. If the number of elements added to the Queue reaches the current capacity, the capacity is automatically increased to accommodate more elements. The capacity can be decreased by calling TrimToSize.

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

Example

[Visual Basic, C#, C++] 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
Imports Microsoft.VisualBasic

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(ControlChars.Tab + "Count:    {0}", myQ.Count)
        Console.Write(ControlChars.Tab + "Values:")
        PrintValues(myQ)
    End Sub
    
    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myCollection.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' 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 )  {
      System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "\t{0}", myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

myQ
    Count:    3
    Values:    Hello    World    !
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

public __gc class SamplesQueue  {

public:
    static void PrintValues(IEnumerable __gc *myCollection) {
        System::Collections::IEnumerator __gc *myEnumerator = myCollection->GetEnumerator();
        while (myEnumerator->MoveNext())
            Console::Write(S"\t{0}", myEnumerator->Current );
        Console::WriteLine();
    }
};

int main()  {

    // Creates and initializes a new Queue.
    Queue __gc *myQ = new Queue();
    myQ->Enqueue(S"Hello");
    myQ->Enqueue(S"World");
    myQ->Enqueue(S"!");

    // Displays the properties and values of the Queue.
    Console::WriteLine(S"myQ");
    Console::WriteLine(S"\tCount:    {0}", __box(myQ->Count));
    Console::Write(S"\tValues:");
    SamplesQueue::PrintValues(myQ);
}
/* 
This code produces the following output.

myQ
Count:    3
Values:    Hello    World    !
*/ 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Collections

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Queue Members | System.Collections Namespace

Show:
© 2014 Microsoft