Generics Queue<T> in C# - Easy Guide with Examples
π Introduction
Hey there, fellow C# learner! π Have you ever waited in a queue at a movie theater or a fast-food counter? You know how it works: first come, first served!
Well, thatβs exactly how Generics Queue<T> in C# works! It follows a FIFO (First-In-First-Out) system, where the first element added is the first to be removed.
Itβs super useful in task scheduling, messaging systems, and customer service applications. Letβs break it down step by step. π
π What You Are Going to Learn in This Lesson
βοΈ What is Generics Queue<T> in C# and why do we need it?
βοΈ How to create, add, and remove elements in a queue.
βοΈ Important Queue<T> methods with examples.
βοΈ A real-world example of queue usage.
βοΈ Understanding FIFO processing in C#.
Why Do We Need Queue<T>?
Queues are important when you need to process elements in order. Here are some real-world examples:
β‘οΈ Call Center System β Calls are answered in the order they arrive.
β‘οΈ Print Queue β The first document sent to the printer gets printed first.
β‘οΈ Task Scheduling β Background tasks are executed in order.
Syntax of Generics Queue<T> in C#
Queue<T> queueName = new Queue<T>();
- T β The type of elements (e.g.,
int
,string
,Employee
). queueName
β The name of the queue.new Queue<T>()
β Creates a new empty queue.
π₯ Example 1: Basic Queue Operations
Letβs create a Queue of Strings and perform basic operations.
Β
π» Code:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// β
Creating a queue of strings
Queue<string> ticketQueue = new Queue<string>();
// β
Adding elements (Enqueue)
ticketQueue.Enqueue("Alice");
ticketQueue.Enqueue("Bob");
ticketQueue.Enqueue("Charlie");
Console.WriteLine("People in the queue:");
foreach (var person in ticketQueue)
{
Console.WriteLine(person);
}
// β
Removing the first element (Dequeue)
string servedPerson = ticketQueue.Dequeue();
Console.WriteLine("\nServed: " + servedPerson);
// β
Checking who is next (Peek)
Console.WriteLine("Next in line: " + ticketQueue.Peek());
}
}
π₯οΈ Output:
People in the queue:
Alice
Bob
Charlie
Served: Alice
Next in line: Bob
π Code Explanation (Step by Step)
Β
1οΈβ£ Creating a Queue
Queue<string> ticketQueue = new Queue<string>();
- We created a
Queue<string>
namedticketQueue
to store names of people waiting in line.
2οΈβ£ Adding People to the Queue (Enqueue)
ticketQueue.Enqueue("Alice");
ticketQueue.Enqueue("Bob");
ticketQueue.Enqueue("Charlie");
- We added three names to the queue.
- The order in the queue is:
Alice β Bob β Charlie
(Alice came first, so she will be served first!)
3οΈβ£ Displaying the Queue
foreach (var person in ticketQueue)
{
Console.WriteLine(person);
}
- This prints all people currently waiting in the queue.
4οΈβ£ Removing the First Person (Dequeue)
string servedPerson = ticketQueue.Dequeue();
Console.WriteLine("\nServed: " + servedPerson);
Dequeue()
removes the first person (Alice) from the queue.- Now the queue looks like this:
Bob β Charlie
5οΈβ£ Checking the Next Person in Line (Peek)
Console.WriteLine("Next in line: " + ticketQueue.Peek());
Peek()
returns the next person without removing them.- Since Alice is gone, Bob is next in line!
π₯ Example 2: Queue of Integers (Processing Tasks)
Let’s simulate a queue of print jobs where each job has a number.
Β
π» Code:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Queue<int> printJobs = new Queue<int>();
printJobs.Enqueue(101);
printJobs.Enqueue(102);
printJobs.Enqueue(103);
Console.WriteLine("Processing print jobs...");
while (printJobs.Count > 0)
{
int job = printJobs.Dequeue();
Console.WriteLine("Printing job: " + job);
}
Console.WriteLine("All print jobs completed!");
}
}
π₯οΈ Output:
Processing print jobs...
Printing job: 101
Printing job: 102
Printing job: 103
All print jobs completed!
π Real-World Example: Call Center System
Imagine a call center where customer calls are handled in order. The first caller should be served first!
Queue<string> callQueue = new Queue<string>();
callQueue.Enqueue("Customer 1");
callQueue.Enqueue("Customer 2");
callQueue.Enqueue("Customer 3");
Console.WriteLine("Now serving: " + callQueue.Dequeue()); // First customer
Console.WriteLine("Next in line: " + callQueue.Peek()); // Who's next?
βοΈ This simulates a customer service system!
π Important Methods of Queue<T>
Method | Description | Example |
---|---|---|
Enqueue(T item) | Adds an item to the queue. | queue.Enqueue("Alice"); |
Dequeue() | Removes and returns the first item. | queue.Dequeue(); |
Peek() | Returns the first item without removing. | queue.Peek(); |
Count | Returns the number of elements. | queue.Count; |
Contains(T item) | Checks if an item exists. | queue.Contains("Alice"); |
Clear() | Removes all items. | queue.Clear(); |
π― Conclusion
We made it! π Now you know how Generics Queue<T> in C# works.
β‘οΈ It follows the FIFO (First-In-First-Out) rule, just like a real-world queue.
β‘οΈ We saw how to add, remove, and peek elements in a queue.
β‘οΈ We explored real-world examples like customer service systems and print jobs.
β‘οΈ You learned important methods like Enqueue(), Dequeue(), Peek(), and Count.
Queues are super useful in programming when you need to process things in order. Now, practice writing your own queue-based programs to reinforce your learning. π
Β
Next What?
Now that you’ve mastered Generics Queue<T> in C#, it’s time to level up! π
π In the next chapter, you will learn about Generics Stack<T> in C#, which works opposite to a queue (LIFO – Last In, First Out)!
Β
Happy coding! ππ₯ Let me know if you have any doubts! π