Represents a first-in, first-out collection of objects.
<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public Class Queue _ Implements ICollection, IEnumerable, ICloneable
Dim instance As Queue
[SerializableAttribute] [ComVisibleAttribute(true)] public class Queue : ICollection, IEnumerable, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class Queue : ICollection, IEnumerable, ICloneable
public class Queue implements ICollection, IEnumerable, ICloneable
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 nullNothingnullptra 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<(Of <(T>)>)
The following example shows how to create and add values to a Queue and how to print out its values.
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 !
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 ! */
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 ! */
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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
Queue q = new Queue();
q.Enqueue("data 1");
q.Enqueue("data 2");
q.Enqueue("data 3");
foreach ( Object var in q )
Console.Write(var.ToString());
IEnumerator ie = q.GetEnumerator();
while ( ie.MoveNext() )
Console.Write(ie.Current.ToString());