C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, the Queue<T>
class is part of the System.Collections.Generic
namespace and represents a first-in, first-out (FIFO) collection of objects. This tutorial will cover the following topics related to the Queue<T>
class:
Let's begin!
To create a new instance of a queue, use the Queue<T>
constructor:
using System.Collections.Generic; Queue<int> myQueue = new Queue<int>();
In this example, we create a queue that will store integers.
To add items to a queue, use the Enqueue
method:
myQueue.Enqueue(10); myQueue.Enqueue(20); myQueue.Enqueue(30);
This will add the integers 10, 20, and 30 to the queue in that order.
To remove an item from a queue, use the Dequeue
method:
int removedItem = myQueue.Dequeue(); Console.WriteLine($"Removed item: {removedItem}"); // Output: Removed item: 10
This will remove the first item from the queue (10 in this case) and return it.
To view the first item in a queue without removing it, use the Peek
method:
int firstItem = myQueue.Peek(); Console.WriteLine($"First item: {firstItem}"); // Output: First item: 20
This will return the first item in the queue (20 in this case) without removing it.
Here are some other common methods provided by the Queue<T>
class:
Count
: Returns the number of items in the queue.int count = myQueue.Count; Console.WriteLine($"Items in the queue: {count}"); // Output: Items in the queue: 2
Clear
: Removes all items from the queue.myQueue.Clear();
Contains
: Determines whether the queue contains a specific item.bool contains = myQueue.Contains(20); Console.WriteLine($"Queue contains 20: {contains}"); // Output: Queue contains 20: True
ToArray
: Copies the queue to a new array.int[] array = myQueue.ToArray();
That's it! You've now learned how to use the Queue<T>
class in C# to create a first-in, first-out (FIFO) collection of objects, add items to a queue, remove items from a queue, peek items in a queue, and use other common methods provided by the Queue<T>
class. The Queue<T>
class is a useful data structure for scenarios where you need to maintain the order of elements and process them in a first-in, first-out manner.
How to use Queue in C#
The Queue
class in C# represents a first-in, first-out (FIFO) collection of objects.
Queue<int> myQueue = new Queue<int>();
Working with queues in C#
Queues are useful for scenarios where elements need to be processed in the order they are added.
myQueue.Enqueue(1); myQueue.Enqueue(2); myQueue.Enqueue(3);
Enqueue and Dequeue operations in C# Queue
Use Enqueue
to add elements and Dequeue
to remove and retrieve elements.
myQueue.Enqueue(4); int value = myQueue.Dequeue(); // Retrieves and removes the element at the beginning
C# Queue class methods and properties
Explore methods like Peek
, Clear
, and properties like Count
in the Queue
class.
int frontElement = myQueue.Peek(); myQueue.Clear(); int queueSize = myQueue.Count;
Using generic Queue<T> in C#
The generic version Queue<T>
allows the queue to store elements of a specific type.
Queue<string> stringQueue = new Queue<string>(); stringQueue.Enqueue("Hello");
Thread safety with Queue in C#
If you need thread safety, consider using ConcurrentQueue<T>
in the System.Collections.Concurrent
namespace.
ConcurrentQueue<int> concurrentQueue = new ConcurrentQueue<int>();
Priority queues in C#
Implementing a priority queue may involve custom logic for element comparison.
PriorityQueue<int> priorityQueue = new PriorityQueue<int>(); priorityQueue.Enqueue(3, 1); // Enqueue element with priority 1
Implementing a circular queue in C#
A circular queue wraps around when it reaches the end.
CircularQueue<int> circularQueue = new CircularQueue<int>(5);
Queue iteration and enumeration in C#
Iterate through a queue using foreach
or convert it to an array for iteration.
foreach (var item in myQueue) { // Process each item }
Using Queue in asynchronous programming in C#
Queues can be used in asynchronous scenarios, ensuring proper synchronization.
async Task ProcessQueueAsync(Queue<int> queue) { while (queue.Count > 0) { int value = queue.Dequeue(); await ProcessValueAsync(value); } }
Queues in parallel programming with Task Parallel Library in C#
Parallel.ForEach
can be used to process elements in parallel.
Parallel.ForEach(myQueue, item => { // Process each item in parallel });