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.
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.
A generic Queue<T> is created by using the Queue<T> class. It stores elements in FIFO order and can grow dynamically.
The following syntax is used to create a generic Queue<T> in C#:
Queue<T> queueName = new Queue<T>();
In this syntax:
The following table demonstrate several method of the Queue<T> Class.
| Method | Description |
|---|---|
| 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. |
| Count | It returns the total number of elements in the queue. |
The following examples demonstrate how to use the Queue<T> class to store and manage elements.
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.
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.
We request you to subscribe our newsletter for upcoming updates.