Queue<T> Class

System.Collections.Generic

Represents a collection of objects that can be accessed by index. The last item added to the collection is the first item retrieved.

This class implements the generic ICollection<T> interface, the generic IEnumerable<T> interface, and the generic IList<T> interface.

Syntax

public sealed class Queue<T> : ICollection<T>, IEnumerable<T>, IList<T>

A Queue<T> is a collection of generic objects that represents a first-in, first-out (FIFO) collection of objects.

The Queue<T> class is a managed, thread-safe implementation of a queue. It is implemented as a dynamic array that resizes as needed.

Type Parameters

Constructors

Name Description
Queue() 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 the keys.
Queue(int capacity) Initializes a new instance of the Queue<T> class that is empty, has the specified initial capacity, and uses the default equality comparer for the type of elements that are the keys.
Queue(IEnumerable<T> collection) Initializes a new instance of the Queue<T> class that contains elements copied from the specified collection, has sufficient capacity to accommodate the number of elements copied, and uses the default equality comparer for the type of elements that are the keys.
Queue(int capacity, IEqualityComparer<T> comparer) Initializes a new instance of the Queue<T> class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer<T>.
Queue(IEnumerable<T> collection, IEqualityComparer<T> comparer) Initializes a new instance of the Queue<T> class that contains elements copied from the specified collection, has sufficient capacity to accommodate the number of elements copied, and uses the specified IEqualityComparer<T>.

Methods

Name Description
Enqueue(T element) Adds an object to the end of the queue.
Dequeue() Removes and returns the object at the beginning of the queue.
Peek() Returns the object at the beginning of the queue without removing it.
Clear() Removes all objects from the queue.
Contains(T element) Determines whether an element is in the queue.
CopyTo(T[] array, int arrayIndex) Copies the queue to a compatible one-dimensional Array, starting at the specified index of the target array.
GetEnumerator() Returns an enumerator that iterates through the queue.
ToArray() Copies the entire queue to a new array.
TrimExcess() Sets the capacity to the actual number of elements that the queue contains.

Properties

Name Description
Count Gets the number of elements contained in the queue.
Capacity Gets or sets the number of elements that the queue can contain at one time.

Remarks

The Queue<T> class represents a first-in, first-out (FIFO) collection of objects.

When you create a Queue<T>, the initial capacity is set to zero or to the capacity specified by the capacity parameter. The capacity automatically increases as elements are added to the collection.

The Enqueue method adds an item to the end of the queue. The Dequeue method removes and returns the item at the beginning of the queue. The Peek method returns the item at the beginning of the queue without removing it.

The Queue<T> class supports generic types. This means you can store any type of object in the queue, and the compiler will check the type safety.

The Queue<T> class is not thread-safe. If you need a thread-safe collection, consider using ConcurrentQueue<T>.

Examples

Example 1: Basic Queue Operations

This example demonstrates the basic operations of enqueueing, dequeuing, and peeking elements in a Queue<string>.


using System;
using System.Collections.Generic;

public class Example
{
    public static void Main(string[] args)
    {
        // Create a new queue of strings
        Queue<string> fruitQueue = new Queue<string>();

        // Enqueue elements
        fruitQueue.Enqueue("Apple");
        fruitQueue.Enqueue("Banana");
        fruitQueue.Enqueue("Cherry");

        Console.WriteLine($"Number of elements in queue: {fruitQueue.Count}");

        // Peek at the front element
        Console.WriteLine($"Front element (Peek): {fruitQueue.Peek()}");

        // Dequeue elements
        Console.WriteLine($"Dequeued: {fruitQueue.Dequeue()}");
        Console.WriteLine($"Dequeued: {fruitQueue.Dequeue()}");

        Console.WriteLine($"Number of elements after dequeuing: {fruitQueue.Count}");

        // Enqueue more elements
        fruitQueue.Enqueue("Date");
        fruitQueue.Enqueue("Elderberry");

        // Print remaining elements by dequeuing
        Console.WriteLine("Remaining elements:");
        while (fruitQueue.Count > 0)
        {
            Console.WriteLine($"- {fruitQueue.Dequeue()}");
        }

        Console.WriteLine($"Final count: {fruitQueue.Count}");
    }
}
            
Note: The Queue<T> class throws an InvalidOperationException if you attempt to Dequeue or Peek on an empty queue.

See Also