Queue Class in C#

Last Updated : 5 Sep, 2026

Queue<T> is a linear data structure that follows the First In First Out (FIFO) principle. It is defined in the System.Collections.Generic namespace and is used to store elements in a way that allows the first inserted element to be removed first.

  • It allows duplicate elements.
  • Its capacity grows dynamically when required.
  • Enqueue(), Dequeue(), and Peek() provide efficient queue operations.

Example: This example demonstrates how to use Queue to enqueue and dequeue elements in FIFO order.

C#
using System;
using System.Collections.Generic;

public class Geeks
{
    public static void Main(string[] args)
    {
        // Create a new queue
        Queue<int> q = new Queue<int>();

        // Enqueue elements into the queue
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(3);
        q.Enqueue(4);

        // Dequeue elements from the queue
        while (q.Count > 0)
        {
            Console.WriteLine(q.Dequeue());
        }
    }
}

Output
1
2
3
4

Explanation:

  • Elements are added to the queue using the Enqueue() method.
  • The Count property is used to check whether the queue contains elements.
  • The Dequeue() method removes and returns the element at the front of the queue.
  • Since Queue follows FIFO, 1 is removed first, followed by 2, 3, and 4.

Declaration

The Queue<T> class can be declared by specifying the type of elements that the queue will store.

Syntax

Queue<T> queueName = new Queue<T>();
Queue<int> numbers = new Queue<int>();

Here, int specifies that the queue can store only integer values.

Hierarchy of Queue Class

The Queue<T> class belongs to the System.Collections.Generic namespace and implements several collection-related interfaces.

  • It implements IEnumerable<T>, ICollection<T>, and IReadOnlyCollection<T> interfaces.
  • It also implements the non-generic IEnumerable and ICollection interfaces.
  • Queue<T> inherits from the Object class.
CSharp-Queue-Hierarchy
Hierarchy of Queue Class

Constructors of Queue<T> Class

The Queue<T> class provides constructors that can be used to create an empty queue, specify its initial capacity, or initialize it with elements from another collection.

1. Queue(): Creates an empty queue with the default initial capacity.

Queue<T> queue = new Queue<T>();

2. Queue(int): Creates an empty queue with the specified initial capacity.

Queue<T> queue = new Queue<T>(10);

3. Queue(IEnumerable<T>): Creates a queue containing elements copied from the specified collection.

Queue<T> queue = new Queue<T>(numbers);

Example: This example demonstrates different ways to initialize a Queue<T>.

C#
using System;
using System.Collections.Generic;

public class Geeks
{
    public static void Main()
    {
        // Create an empty queue
        Queue<int> queue1 = new Queue<int>();

        // Create a queue with initial capacity
        Queue<int> queue2 = new Queue<int>(5);

        // Create a queue from another collection
        int[] numbers = { 10, 20, 30 };
        Queue<int> queue3 = new Queue<int>(numbers);

        Console.WriteLine("Queue 1 Count: " + queue1.Count);
        Console.WriteLine("Queue 2 Count: " + queue2.Count);
        Console.WriteLine("Queue 3 Count: " + queue3.Count);
    }
}

Output
Queue 1 Count: 0
Queue 2 Count: 0
Queue 3 Count: 3

Performing Different Operations on Queue Class

1. Adding Elements

With the help of the Enqueue() method, we can add an element to the queue. The Enqueue() method places the element at the end of the queue.

C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Creating an empty Queue
        Queue<string> queue = new Queue<string>();

        // Adding elements to the Queue
        queue.Enqueue("Geeks");
        queue.Enqueue("For");
        queue.Enqueue("Geeks");

        // Displaying the Queue
        foreach (string item in queue)
        {
            Console.WriteLine(item);
        }
    }
}

Output
Geeks
For
Geeks

2. Accessing the Element

With the help of the Peek() method, we can access the element at the front of the queue without removing it.

C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Creating an empty Queue
        Queue<string> queue = new Queue<string>();

        // Adding elements to the Queue
        queue.Enqueue("Welcome");
        queue.Enqueue("To");
        queue.Enqueue("Geeks");
        queue.Enqueue("For");
        queue.Enqueue("Geeks");

        // Displaying the Queue
        Console.WriteLine("Initial Queue:");

        foreach (string item in queue)
        {
            Console.WriteLine(item);
        }

        // Accessing the front element
        Console.WriteLine(
            "The element at the front of the queue is: "
            + queue.Peek());

        // Displaying the Queue again
        Console.WriteLine("Final Queue:");

        foreach (string item in queue)
        {
            Console.WriteLine(item);
        }
    }
}

Output
Initial Queue:
Welcome
To
Geeks
For
Geeks
The element at the front of the queue is: Welcome
Final Queue:
Welcome
To
Geeks
For
Geeks

3. Removing Elements

With the help of the Dequeue() method, we can remove and return the element from the front of the queue.

C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Creating an empty Queue
        Queue<int> queue = new Queue<int>();

        // Adding elements to the Queue
        queue.Enqueue(10);
        queue.Enqueue(15);
        queue.Enqueue(30);
        queue.Enqueue(20);
        queue.Enqueue(5);

        // Displaying the Queue
        Console.WriteLine("Initial Queue:");

        foreach (int item in queue)
        {
            Console.WriteLine(item);
        }

        // Removing elements using Dequeue()
        Console.WriteLine("Dequeued element: " + queue.Dequeue());
        Console.WriteLine("Dequeued element: " + queue.Dequeue());

        // Displaying the Queue after Dequeue operation
        Console.WriteLine("Queue after Dequeue operation:");

        foreach (int item in queue)
        {
            Console.WriteLine(item);
        }

        // Checking whether the Queue is empty
        Console.WriteLine("Is queue empty? " + (queue.Count == 0));
    }
}

Output
Initial Queue:
10
15
30
20
5
Dequeued element: 10
Dequeued element: 15
Queue after Dequeue operation:
30
20
5
Is queue empty? False

4. Checking the Number of Elements

The Count property is used to determine the number of elements currently present in the queue.

C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Creating an empty Queue
        Queue<int> queue = new Queue<int>();

        // Adding elements
        queue.Enqueue(10);
        queue.Enqueue(20);
        queue.Enqueue(30);
        queue.Enqueue(40);
        queue.Enqueue(50);

        // Displaying the number of elements
        Console.WriteLine("Number of elements: " + queue.Count);
    }
}

Output
Number of elements: 5

5. Removing All Elements

With the help of the Clear() method, we can remove all elements from the queue.

C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        Queue<int> queue = new Queue<int>();

        queue.Enqueue(10);
        queue.Enqueue(20);
        queue.Enqueue(30);
        queue.Enqueue(40);

        Console.WriteLine(
            "Elements before Clear: " + queue.Count);

        queue.Clear();

        Console.WriteLine(
            "Elements after Clear: " + queue.Count);
    }
}

Output
Elements before Clear: 4
Elements after Clear: 0

Properties

The Queue<T> class provides properties that allow users to obtain information about the queue.

Count: Returns the number of elements currently contained in the Queue<T> object.

C#
using System;
using System.Collections.Generic;

Queue<int> queue = new Queue<int>();

queue.Enqueue(10);
queue.Enqueue(20);
queue.Enqueue(30);

Console.WriteLine(queue.Count);

Output
3

Methods in Queue<T> Class

The Queue<T> class provides methods for adding, accessing, removing, and managing elements in the queue.

  • Clear(): Removes all elements from the queue.
  • Contains(T): Determines whether the specified element is present in the queue.
  • CopyTo(T[], Int32): Copies the queue elements to an existing array, starting at the specified index.
  • Dequeue(): Removes and returns the element at the beginning of the queue.
  • Enqueue(T): Adds an element to the end of the queue.
  • Peek(): Returns the element at the beginning of the queue without removing it.
  • ToArray(): Copies the queue elements to a new array.
  • TrimExcess(): Reduces the capacity of the queue when appropriate.
  • TryDequeue(out T): Attempts to remove and return the element at the beginning of the queue.
  • TryPeek(out T): Attempts to return the element at the beginning of the queue without removing it.

Applications of Queue<T>

Queue<T> is useful when elements need to be processed in the same order in which they are added.

  • Task Scheduling: Manages tasks that need to be processed sequentially.
  • Print Queue: Stores print jobs and processes them in the order they are received.
  • Breadth-First Search (BFS): Stores vertices that need to be visited level by level.
  • Request Processing: Manages incoming requests in the order they are received.
  • Data Buffering: Temporarily stores data that needs to be processed sequentially.
Comment

Explore