C# Queue<T>

Last Updated : 2 Jul 2026

The Queue<T> class is used to store and manage elements in First In, First Out (FIFO) order. It is useful when the first element added to the collection should be the first one removed.

In this chapter, you will learn what the Queue<T> class is, how to create and use it, its commonly used methods, and practical examples.

What is Queue<T> in C#?

The Queue<T> class is available in the System.Collections.Generic namespace and is used to store a collection of elements in FIFO (First In, First Out) order. New elements are added to the end of the queue by using the Enqueue() method, and elements are removed from the front by using the Dequeue() method. Unlike HashSet<T> or SortedSet<T>, a queue allows duplicate values.

Creating a Generic Queue<T> in C#

A generic Queue<T> is created by using the Queue<T> class. It stores elements in FIFO order and can grow dynamically.

Syntax

The following syntax is used to create a generic Queue<T> in C#:

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

In this syntax:

  • Queue<T> specifies the generic queue class.
  • T specifies the data type of the elements, such as int, string, or char.
  • queueName specifies the name of the queue object.
  • new Queue<T>() creates an empty queue.

Common Methods of the Queue<T> Class

The following table demonstrate several method of the Queue<T> Class.

MethodDescription
Enqueue()It adds an element to the end of the queue.
Dequeue()It removes and returns the element at the front of the queue.
Peek()It returns the first element without removing it.
Contains()It checks whether an element exists in the queue.
Clear()It removes all elements from the queue.
CountIt returns the total number of elements in the queue.

Examples of the Queue<T> Class

The following examples demonstrate how to use the Queue<T> class to store and manage elements.

Example 1: Adding and Removing Elements from a Queue<T>

This example shows how to add elements by using the Enqueue() method, access the first element by using the Peek() method, and remove elements by using the Dequeue() method.

using System;
using System.Collections.Generic;

public class QueueExample
{
    public static void Main(string[] args)
    {
        Queue<string> names = new Queue<string>();
        names.Enqueue("Sonoo");
        names.Enqueue("Peter");
        names.Enqueue("James");
        names.Enqueue("Ratan");
        names.Enqueue("Irfan");

        foreach (string name in names)
        {
            Console.WriteLine(name);
        }

        Console.WriteLine("Peek element: "+names.Peek());
        Console.WriteLine("Dequeue: "+ names.Dequeue());
        Console.WriteLine("After Dequeue, Peek element: " + names.Peek());
    }
}

Output:

Sonoo
Peter
James
Ratan
Irfan
Peek element: Sonoo
Dequeue: Sonoo
After Dequeue, Peek element: Peter

Explanation

In this example, we create a Queue<string> and add five names by using the Enqueue() method. Next, the Peek() method displays the first element without removing it. The Dequeue() method then removes the first element from the queue, and another call to Peek() shows the new first element.

Example 2: Creating a Queue<T> by Using a Collection Initializer

This example demonstrates how to create and initialize a Queue<T> by using a collection initializer and display its elements with a foreach loop.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Queue<int> numbers = new Queue<int>(new[] { 10, 20, 30, 40, 50 });

        Console.WriteLine("Queue Elements:");

        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}

Output:

Queue Elements:
10
20
30
40
50

Explanation

In this example, we create a Queue<int> and initialize it with five integer values. After that, we use a foreach loop to iterate through the queue and display each element. The elements are printed in the same order in which they were added because a Queue<T> follows the First In, First Out (FIFO) principle.


Next TopicC# LinkedList