C# Queue<T>

Summary: in this tutorial, you will learn how to use the C# Queue<T> class to manage a collection of objects based on the first-in, first-out (FIFO) order.

Introduction to the C# Queue<T> class

Imagine you’re organizing a tech conference. At this conference, attendees need to check in at the registration desk. To make the check-in process more smooth and efficient, you decide to implement a check-in queue.

As attendees arrive, they join the queue, forming a line. The first person to arrive is at the front of the queue, and the last person to arrive is at the rear.

The registration desk personnel process the attendees in a first-in, first-out ( FIFO) order. They start with the person at the front of the queue, verify their identification, and provide them with their conference badges.

As each person is checked in and receives their badge, they move to the front of the line and exit the queue. The next person in the queue then moves up to the front and proceeds with the check-in process.

This continues until all attendees have been checked in, and the queue becomes empty.

C# Queue<T> class works the same as the queue of attendees. It has two main methods:

  • Enqueue() – adds an object to the end of the queue.
  • Dequeue() – removes and returns an object at the beginning of the queue.

The Queue<T> class also has the Count property that returns the number of elements of the queue.

C# Queue<T> class example

The following example illustrates how to use the C# Queue<T> class to manage a queue of attendees:

using static System.Console;

var attendees = new List<string>{
    "John",
    "Jane",
    "Bob", 
    "Alice"
};

var queue = new Queue<string>();

foreach (var attendee in attendees)
{
    queue.Enqueue(attendee);
    WriteLine($"{attendee} has arrived.");
}

WriteLine($"There are {queue.Count} attendees in the queue");


while(queue.Count > 0)
{
    var attendee = queue.Dequeue();
    WriteLine($"Checking in {attendee} ...");
}

WriteLine($"There are {queue.Count} attendees in the queue");Code language: C# (cs)

Output:

John has arrived.
Jane has arrived.
Bob has arrived.
Alice has arrived.
There are 4 attendees in the queue
Checking in John ...
Checking in Jane ...
Checking in Bob ...
Checking in Alice ...
There are 0 attendees in the queueCode language: plaintext (plaintext)

How it works.

First, create a list of four attendees:

var attendees = new List<string>{
    "John",
    "Jane",
    "Bob", 
    "Alice"
};Code language: C# (cs)

Second, create a queue of strings to manage the attendees:

var queue = new Queue<string>();Code language: C# (cs)

Third, iterate the attendees list and enqueue each of them using the Enqueue() method:

foreach (var attendee in attendees)
{
    queue.Enqueue(attendee);
    WriteLine($"{attendee} has arrived.");
}Code language: C# (cs)

Fourth, get the number of attendees using the Count property and display it to the console:

WriteLine($"There are {queue.Count} attendees in the queue");Code language: C# (cs)

Fifth, remove each attendee until the queue is empty using the Dequeue() method:

while(queue.Count > 0)
{
    var attendee = queue.Dequeue();
    WriteLine($"Checking in {attendee} ...");
}Code language: C# (cs)

Finally, show the number of attendees in the queue to the console:

WriteLine($"There are {queue.Count} attendees in the queue");Code language: C# (cs)

Summary

  • Use C# Queue<T> class to manage a collection of objects in the first-in, first-out (FIFO) order.
Was this tutorial helpful ?