Queue<T> Class
System.Collections.Generic
| Inheritance | object System.Collections.Generic.Queue<T> |
|---|---|
| Implements | System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection, System.Collections.IEnumerable |
| Derived classes | None |
| File attributes |
Represents a first-in, first-out (FIFO) collection of objects.
Remarks
The T is the type of elements in the queue.
A Queue<T> can be used to contain any type of managed object. The capacity of a Queue<T> automatically adjusts as elements are added or removed.
Elements are added to the end and removed from the beginning of the queue. This allows the Queue<T> to function as a FIFO collection.
All members of Queue<T> are thread-safe and can be used concurrently from multiple threads.
Constructors
-
Queue<T>()
Initializes a new instance of the
Queue<T>class that is empty, has the default initial capacity, and uses the default equality comparer for the type of elements that are to be stored in the list. -
Queue<T>(Int32)
Initializes a new instance of the
Queue<T>class that is empty and has the specified initial capacity or the default initial capacity if the specified initial capacity is a value less than one.- capacity: System.Int32
The initial number of elements that the queue can contain.
- capacity: System.Int32
-
Queue<T>(IEnumerable<T>)
Initializes a new instance of the
Queue<T>class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.- collection: System.Collections.Generic.IEnumerable<T>
TheIEnumerable<T>whose elements are copied to the new queue.
- collection: System.Collections.Generic.IEnumerable<T>
Methods
-
Clear() void
Removes all elements from the
Queue<T>. -
Contains(T) bool
Determines whether an element is in the
Queue<T>.- item: T
The object to locate in theQueue<T>. The value can benullfor reference types.
- item: T
-
CopyTo(T[], Int32) void
Copies the
Queue<T>elements to a one-dimensionalArray, starting at the specified array index.- array: T[]
The one-dimensionalArraythat is the destination of the elements copied fromQueue<T>. Must have zero-based indexing. - arrayIndex: System.Int32
The zero-based index inarrayat which the copying begins.
- array: T[]
-
Dequeue() T
Removes and returns the object at the beginning of the
Queue<T>. -
Enqueue(T) void
Adds an object to the end of the
Queue<T>.- item: T
The object to add to theQueue<T>. The value can benullfor reference types.
- item: T
-
GetEnumerator() IEnumerator<T>
Returns an enumerator that iterates through the collection.
-
Peek() T
Returns the object at the beginning of the
Queue<T>without removing it. -
ToArray() T[]
Copies the elements to a new array and returns it.
-
TrimExcess() void
Sets the capacity to the actual number of elements that the
Queue<T>can hold. This member is not relevant forQueue<T>, as its capacity automatically increases as needed.
Properties
-
Count int { get; }
Gets the number of elements contained in the
Queue<T>.
Interface Implementations
-
System.Collections.Generic.IEnumerable<T>.GetEnumerator() IEnumerator<T>
Returns an enumerator that iterates through the collection.
-
System.Collections.Generic.IReadOnlyCollection<T>.GetEnumerator() IEnumerator<T>
Returns an enumerator that iterates through the collection.
-
System.Collections.ICollection.CopyTo(Array, Int32) void
Copies the
ICollectionelements to a one-dimensionalArray, starting at the specified array index. -
System.Collections.ICollection.IsSynchronized bool { get; }
Gets a value indicating whether access to the
ICollectionis synchronized (thread-safe). -
System.Collections.ICollection.SyncRoot object { get; }
Gets an object that can be used to synchronize access to the
ICollection. -
System.Collections.IEnumerable.GetEnumerator() IEnumerator
Returns an enumerator that iterates through a collection.
Example
The following code example demonstrates how to use the Queue<T> class to create, populate, and process a queue.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a queue of strings.
Queue<string> myQueue = new Queue<string>();
// Add elements to the queue.
myQueue.Enqueue("Hello");
myQueue.Enqueue("World");
myQueue.Enqueue("!");
// Display the number of elements in the queue.
Console.WriteLine("Number of elements in the queue: {0}", myQueue.Count);
// Peek at the first element.
Console.WriteLine("First element is: {0}", myQueue.Peek());
// Dequeue and display elements.
while (myQueue.Count > 0)
{
string element = myQueue.Dequeue();
Console.WriteLine("Dequeued: {0}", element);
}
// Display the number of elements after dequeuing.
Console.WriteLine("Number of elements after dequeuing: {0}", myQueue.Count);
}
}