In C#, a Queue
is a collection class that follows the First-In-First-Out (FIFO) principle. This means that the first item added to the queue is the first item removed, similar to a line of people at a bank—each person waits their turn in the order they arrived. Queue
is useful for tasks like handling tasks in the order they’re received, managing events, or simulating real-world queues in programming.
A Queue
in C# is part of the System.Collections.Generic
namespace and follows the FIFO order, making it suitable for managing tasks, events, and sequential data processing. You can add elements to the end of the queue, and the element at the beginning is the first one to be removed. Queue
operations are efficient for scenarios requiring orderly processing where each item must be handled in the order it arrived.
Creating a queue in C# is straightforward, and it can store any type of data. Here’s an example of creating an empty queue of integers and one initialized with strings.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Creating an empty Queue of integers
Queue<int> numberQueue = new Queue<int>();
// Initializing a Queue with values
Queue<string> customerQueue = new Queue<string>(new List<string> { "Alice", "Bob", "Charlie" });
Console.WriteLine("Queue initialized with customers:");
foreach (var customer in customerQueue)
{
Console.WriteLine(customer);
}
}
}
Enqueue
)The Enqueue
method adds an element to the end of the queue.
numberQueue.Enqueue(10);
numberQueue.Enqueue(20);
numberQueue.Enqueue(30);
Peek
)The Peek
method allows you to view the item at the front of the queue without removing it.
int frontElement = numberQueue.Peek(); // Output: 10
Console.WriteLine($"Front element: {frontElement}");
Dequeue
)The Dequeue
method removes and returns the item at the front of the queue.
int removedElement = numberQueue.Dequeue(); // Output: 10
Console.WriteLine($"Removed element: {removedElement}");
Queue
can hold tasks in the order they are received, ensuring that each task is handled in the correct sequence.Imagine a task processing system where tasks are added to a queue as they arrive. Each task is processed in the order it was added, which is essential in scenarios like customer service, where the first customer request should be handled first.
using System;
using System.Collections.Generic;
class TaskProcessing
{
private Queue<string> taskQueue = new Queue<string>();
public void AddTask(string task)
{
taskQueue.Enqueue(task);
Console.WriteLine($"Added task: {task}");
}
public void ProcessTask()
{
if (taskQueue.Count > 0)
{
string taskToProcess = taskQueue.Dequeue();
Console.WriteLine($"Processing task: {taskToProcess}");
}
else
{
Console.WriteLine("No tasks to process.");
}
}
public void ShowTasks()
{
Console.WriteLine("\nCurrent Tasks in Queue:");
foreach (var task in taskQueue)
{
Console.WriteLine(task);
}
}
}
class Program
{
static void Main()
{
TaskProcessing taskProcessing = new TaskProcessing();
// Adding tasks
taskProcessing.AddTask("Task 1 - Customer Support");
taskProcessing.AddTask("Task 2 - IT Support");
taskProcessing.AddTask("Task 3 - Maintenance");
// Process tasks in FIFO order
taskProcessing.ProcessTask(); // Processes "Task 1 - Customer Support"
taskProcessing.ProcessTask(); // Processes "Task 2 - IT Support"
// Show remaining tasks
taskProcessing.ShowTasks();
}
}
AddTask
: This method adds a new task to the Queue
, simulating a customer request or a support task.ProcessTask
: This method processes tasks in the order they were added by dequeuing the front task.ShowTasks
: Displays all tasks currently waiting in the queue.In this setup, each task is processed in a predictable and orderly fashion, helping to manage service requests in an organized way.
Queue
follows First-In-First-Out, which ensures that the first item added is the first to be removed.Queue
provides Enqueue for adding, Dequeue
for removing, and Peek
for viewing the front item without removing it.Queue
is ideal for scenarios where tasks or events need to be processed in the order they arrive.The Queue
class in C# offers a powerful way to manage collections of items in a sequential, FIFO order. This is crucial for tasks that need to maintain the order of processing, such as customer service queues, task schedulers, and event handling systems. With methods like Enqueue
, Dequeue
, and Peek
, Queue
provides essential functionality for creating structured, orderly workflows. This knowledge will allow developers to effectively implement structured data processing in C# applications.