Export (0) Print
Expand All

Queue Class

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

System.Object
  System.Collections.Queue

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

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<DebuggerDisplayAttribute("Count = {Count}")> _
Public Class Queue _
	Implements ICollection, IEnumerable, ICloneable

The Queue type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkQueueInitializes a new instance of the Queue class that is empty, has the default initial capacity, and uses the default growth factor.
Public methodSupported by the XNA FrameworkQueue(ICollection)Initializes a new instance of the Queue class that contains elements copied from the specified collection, has the same initial capacity as the number of elements copied, and uses the default growth factor.
Public methodSupported by the XNA FrameworkQueue(Int32)Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the default growth factor.
Public methodSupported by the XNA FrameworkQueue(Int32, Single)Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the specified growth factor.
Top

  NameDescription
Public propertySupported by the XNA FrameworkCountGets the number of elements contained in the Queue.
Public propertySupported by the XNA FrameworkIsSynchronizedGets a value indicating whether access to the Queue is synchronized (thread safe).
Public propertySupported by the XNA FrameworkSyncRootGets an object that can be used to synchronize access to the Queue.
Top

  NameDescription
Public methodSupported by the XNA FrameworkClearRemoves all objects from the Queue.
Public methodSupported by the XNA FrameworkCloneCreates a shallow copy of the Queue.
Public methodSupported by the XNA FrameworkContainsDetermines whether an element is in the Queue.
Public methodSupported by the XNA FrameworkCopyToCopies the Queue elements to an existing one-dimensional Array, starting at the specified array index.
Public methodSupported by the XNA FrameworkDequeueRemoves and returns the object at the beginning of the Queue.
Public methodSupported by the XNA FrameworkEnqueueAdds an object to the end of the Queue.
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetEnumeratorReturns an enumerator that iterates through the Queue.
Public methodSupported by the XNA FrameworkGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkPeekReturns the object at the beginning of the Queue without removing it.
Public methodStatic memberSynchronizedReturns a new Queue that wraps the original queue, and is thread safe.
Public methodSupported by the XNA FrameworkToArrayCopies the Queue elements to a new array.
Public methodSupported by the XNA FrameworkToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkTrimToSizeSets the capacity to the actual number of elements in the Queue.
Top

  NameDescription
Public Extension MethodAsParallelOverloaded. Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsParallelOverloaded. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableOverloaded. Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodAsQueryableOverloaded. (Defined by Queryable.)
Public Extension MethodSupported by the XNA FrameworkCast(Of TResult)Overloaded. Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by the XNA FrameworkCast(Of TResult)Overloaded. (Defined by Enumerable.)
Public Extension MethodSupported by the XNA FrameworkOfType(Of TResult)Overloaded. Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Public Extension MethodSupported by the XNA FrameworkOfType(Of TResult)Overloaded. (Defined by Enumerable.)
Top

This class implements a queue as a circular array. Objects stored in a Queue are inserted at one end and removed from the other.

Queues and stacks are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use Queue if you need to access the information in the same order that it is stored in the collection. Use Stack if you need to access the information in reverse order. Use ConcurrentQueue(Of T) or ConcurrentStack(Of T) if you need to access the collection from multiple threads concurrently.

Three main operations can be performed on a Queue and its elements:

  • Enqueue adds an element to the end of the Queue.

  • Dequeue removes the oldest element from the start of the Queue.

  • Peek returns the oldest element that is at the start of the Queue but does not remove it from the Queue.

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 Nothing 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    !

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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.

Show:
© 2014 Microsoft