Complete C# Tutorial

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. πŸš€

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>();
				
			
  1. We created a Queue<string> named ticketQueue to store names of people waiting in line.

2️⃣ Adding People to the Queue (Enqueue)

				
					ticketQueue.Enqueue("Alice");
ticketQueue.Enqueue("Bob");
ticketQueue.Enqueue("Charlie");
				
			
  1. We added three names to the queue.
  2. 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);
}
				
			
  1. This prints all people currently waiting in the queue.

4️⃣ Removing the First Person (Dequeue)

				
					string servedPerson = ticketQueue.Dequeue();
Console.WriteLine("\nServed: " + servedPerson);
				
			
  1. Dequeue() removes the first person (Alice) from the queue.
  2. Now the queue looks like this:
				
					Bob β†’ Charlie
				
			

5️⃣ Checking the Next Person in Line (Peek)

				
					Console.WriteLine("Next in line: " + ticketQueue.Peek());
				
			
  1. Peek() returns the next person without removing them.
  2. 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>

MethodDescriptionExample
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();
CountReturns 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! πŸš€

Leave a Comment

Share this Doc

Generic Queue<T>

Or copy link