5 out of 7 rated this helpful - Rate this topic

Queue Class

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

System.Object
  System.Collections.Queue

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class Queue : ICollection, IEnumerable, 
	ICloneable

The Queue type exposes the following members.

  Name Description
Public method Supported by the XNA Framework Queue() Initializes a new instance of the Queue class that is empty, has the default initial capacity, and uses the default growth factor.
Public method Supported by the XNA Framework Queue(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 method Supported by the XNA Framework Queue(Int32) Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the default growth factor.
Public method Supported by the XNA Framework Queue(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
  Name Description
Public property Supported by the XNA Framework Count Gets the number of elements contained in the Queue.
Public property Supported by the XNA Framework IsSynchronized Gets a value indicating whether access to the Queue is synchronized (thread safe).
Public property Supported by the XNA Framework SyncRoot Gets an object that can be used to synchronize access to the Queue.
Top
  Name Description
Public method Supported by the XNA Framework Clear Removes all objects from the Queue.
Public method Supported by the XNA Framework Clone Creates a shallow copy of the Queue.
Public method Supported by the XNA Framework Contains Determines whether an element is in the Queue.
Public method Supported by the XNA Framework CopyTo Copies the Queue elements to an existing one-dimensional Array, starting at the specified array index.
Public method Supported by the XNA Framework Dequeue Removes and returns the object at the beginning of the Queue.
Public method Supported by the XNA Framework Enqueue Adds an object to the end of the Queue.
Public method Supported by the XNA Framework Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework GetEnumerator Returns an enumerator that iterates through the Queue.
Public method Supported by the XNA Framework GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework Peek Returns the object at the beginning of the Queue without removing it.
Public method Static member Synchronized Returns a Queue wrapper that is synchronized (thread safe).
Public method Supported by the XNA Framework ToArray Copies the Queue elements to a new array.
Public method Supported by the XNA Framework ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Supported by the XNA Framework TrimToSize Sets the capacity to the actual number of elements in the Queue.
Top
  Name Description
Public Extension Method AsParallel Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Supported by the XNA Framework Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method Supported by the XNA Framework OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

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 null as a valid value and allows duplicate elements.

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

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


 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    !
*/ 


.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
You need a list, not a queue
What you're looking for is not a queue.  Sounds like you need a list overridden with push and pop methods.
Too restrictive
It seems to me that this class is trying too hard to be pure.  It won't let me edit any item (even the last queued), and it won't let me walk the queue (even though it makes the contents available via ToArray()).  If this was a computer science project I would understand, and I do appreciate that the API should encourage efficient use, but why does it stand in the way of efficiently using it for real-world problems?

(I use it to store a queue of busy (odd entries) and idle (even entries) durations.  As my process is busy or idle it enqueues times.  I want to edit the last queued item because two busy periods may abutt.  Occasionally I want to walk the queue to calculate proportion of time busy over different periods.)

==> Sounds like you need to use a list.  Possibly override it to provide push/pop functionality.  The Queue, as is, works perfectly for many applications.