0
0
CsharpHow-ToBeginner · 3 min read

How to Implement Queue in C#: Syntax and Examples

In C#, you can implement a queue using the built-in Queue<T> class which follows the FIFO (First In, First Out) principle. Use Enqueue() to add items and Dequeue() to remove items from the queue.
📐

Syntax

The Queue<T> class in C# is a generic collection that stores elements in a first-in, first-out order. You create a queue by specifying the type of elements it will hold inside angle brackets <>. Use Enqueue(item) to add an item to the end of the queue, and Dequeue() to remove and return the item at the front.

Other useful methods include Peek() to look at the front item without removing it, and Count to get the number of items.

csharp
Queue<T> queue = new Queue<T>();
queue.Enqueue(item); // Add item
T firstItem = queue.Dequeue(); // Remove and get first item
T peekItem = queue.Peek(); // Get first item without removing
int size = queue.Count; // Number of items
💻

Example

This example shows how to create a queue of strings, add items, remove them, and check the queue size.

csharp
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Queue<string> queue = new Queue<string>();

        // Add items
        queue.Enqueue("apple");
        queue.Enqueue("banana");
        queue.Enqueue("cherry");

        Console.WriteLine($"Queue size: {queue.Count}");

        // Peek at the first item
        Console.WriteLine($"First item: {queue.Peek()}");

        // Remove items
        while (queue.Count > 0)
        {
            string item = queue.Dequeue();
            Console.WriteLine($"Removed: {item}");
        }

        Console.WriteLine($"Queue size after removals: {queue.Count}");
    }
}
Output
Queue size: 3 First item: apple Removed: apple Removed: banana Removed: cherry Queue size after removals: 0
⚠️

Common Pitfalls

  • Dequeue on empty queue: Calling Dequeue() when the queue is empty throws an InvalidOperationException. Always check Count before dequeuing.
  • Peek on empty queue: Similarly, Peek() throws an exception if the queue is empty.
  • Confusing queue order: Remember that queues remove items in the order they were added (FIFO), not last-in-first-out.
csharp
Queue<int> queue = new Queue<int>();

// Wrong: Dequeue without checking
// int item = queue.Dequeue(); // Throws exception

// Right: Check before dequeue
if (queue.Count > 0)
{
    int item = queue.Dequeue();
}
📊

Quick Reference

Method/PropertyDescription
Enqueue(item)Adds an item to the end of the queue
Dequeue()Removes and returns the item at the front
Peek()Returns the item at the front without removing it
CountGets the number of items in the queue
Clear()Removes all items from the queue

Key Takeaways

Use Queue in C# to implement a FIFO collection easily.
Always check Count before calling Dequeue or Peek to avoid exceptions.
Enqueue adds items to the end; Dequeue removes from the front.
Peek lets you see the next item without removing it.
Queue provides simple methods to manage ordered data efficiently.