C# Queue

Beginner
7 min read
Updated: Nov 2026
3 examples
Queue

Definition and Usage

In C#, Queue<T> is a collection that works like a line. It is first in, first out (FIFO): the item that joined first leaves first. Think of people at a ticket counter or print jobs waiting for a printer. You add at the back with Enqueue(), remove from the front with Dequeue(), and read the front without removing it with Peek().

The Count property tells you how many items are waiting. A queue allows duplicates and, for reference types, null. It has no indexer (you cannot write line[0]), but foreach, ToArray(), and CopyTo() all go from the front to the back, so the order is fixed and predictable. It lives in System.Collections.Generic, so add using System.Collections.Generic; at the top of your file.

The Queue Methods index lists every method guide in this series.

Empty queue warning.

Dequeue() and Peek() throw InvalidOperationException when the queue is empty. Check Count first, or use TryDequeue and TryPeek in newer .NET. Also, Queue<T> is not thread-safe; use ConcurrentQueue<T> when several threads share one queue.

C# Syntax

Common first steps:

C#
using System.Collections.Generic;

Queue<string> line = new Queue<string>();
line.Enqueue("Asha");                  // join at the back
line.Enqueue("Ben");
string front = line.Peek();            // "Asha" (not removed)
string served = line.Dequeue();        // "Asha" (removed)
int total = line.Count;                // 1

// Start with items already in the line:
Queue<int> tokens = new Queue<int>(new int[] { 1, 2, 3 });

Queue vs List, Stack, and ConcurrentQueue

Pick the collection that matches how items should come out:

TypeOrder of removalAccessGood for
Queue<T>First in, first out (oldest first)Add at back, take from front, no indexerWaiting lines, print jobs, tasks to process in arrival order
List<T>Any item, by indexRead, insert, and remove anywhereAn ordered list you change in many places (see List)
Stack<T>Last in, first out (newest first)Push and pop at the top onlyUndo history, going back; it is the opposite of a queue (see Stack)
ConcurrentQueue<T>First in, first outThread-safe add and removeA queue shared by several threads

Dequeue vs TryDequeue

Both take the item at the front. They differ when the queue might be empty:

CallQueue has itemsQueue is emptyAvailable in
Dequeue()Removes and returns the front itemThrows InvalidOperationExceptionAll .NET versions
TryDequeue(out x)Returns true, x is the front itemReturns false, x is the default valueNewer .NET (.NET Core 2.0+)
Peek()Returns the front item, keeps itThrows InvalidOperationExceptionAll .NET versions
TryPeek(out x)Returns true, x is the front itemReturns false, x is the default valueNewer .NET (.NET Core 2.0+)

C# Examples

Example 1 — Enqueue, Peek, and Dequeue

A first program: three people join a line, you look at the front, serve one person, and check how many are left.

C#
using System;
using System.Collections.Generic;

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

        line.Enqueue("Asha");
        line.Enqueue("Ben");
        line.Enqueue("Chitra");
        Console.WriteLine(line.Count);

        Console.WriteLine(line.Peek());
        Console.WriteLine(line.Dequeue());

        Console.WriteLine(line.Count);
        Console.WriteLine(line.Peek());
    }
}

How It Works

  1. One type argument. Queue<string> means every item in the queue is a string. There are no keys, only a line of items.
  2. Join at the back. Each Enqueue call adds a name behind the others, so after three calls Count is 3 and Asha is first.
  3. Look, then take. Peek() shows Asha without removing her, so the second line prints Asha too. Dequeue() then removes her and returns her name, and the third line prints Asha once more.
  4. The line moves up. Now Count is 2 and the new front is Ben, which Peek() confirms. That is first in, first out.

Example 2 — Take From a Queue Without a Crash

Serve one person using a Count check, then ask again with TryDequeue when nobody is left.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<string> line = new Queue<string>();
        line.Enqueue("Asha");

        if (line.Count > 0) {
            Console.WriteLine("Serving " + line.Dequeue());
        }

        if (line.TryDequeue(out string next)) {
            Console.WriteLine("Serving " + next);
        } else {
            Console.WriteLine("Nobody is waiting");
        }
    }
}

How It Works

  1. The risk. Calling Dequeue() on an empty queue throws InvalidOperationException, so a bare call is only safe when you know someone is waiting.
  2. Way one: check Count. The queue has one name, so line.Count > 0 is true. The call runs and prints Serving Asha, and the queue is empty afterward.
  3. Way two: TryDequeue. It returns false for an empty queue instead of throwing, so the code takes the else branch and prints Nobody is waiting.
  4. Version note. TryDequeue is in newer .NET (.NET Core 2.0 and later). On classic .NET Framework, use the Count way.

Example 3 — Take Turns With Dequeue and Enqueue

Three players share a game. Whoever plays goes to the back of the line, so the turns repeat in the same order.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<string> players = new Queue<string>(new string[] { "Asha", "Ben", "Chitra" });

        for (int turn = 1; turn <= 5; turn++) {
            string who = players.Dequeue();
            Console.WriteLine("Turn " + turn + ": " + who);
            players.Enqueue(who);
        }

        Console.WriteLine(string.Join(", ", players));
    }
}

How It Works

  1. Start from an array. The constructor copies the names in order, so Asha is at the front and Chitra is at the back.
  2. Serve, then re-join. Each turn takes the front player with Dequeue(), prints the name, and puts the same name back at the end with Enqueue(who).
  3. A circle appears. Because nobody leaves for good, the order repeats: Asha, Ben, Chitra, then Asha and Ben again for turns 4 and 5.
  4. Final order. After five turns, Chitra is next, then Asha, then Ben. string.Join walks the queue front to back, so the printed order is always the same.

Next: C# Queue Methods Index

After you know what a Queue is for, open the method index for a full tutorial on each Queue API.

Start withWhy
Enqueue()Add an item at the back of the line
Dequeue()Take the item at the front, and handle an empty queue
Peek()Look at the front item without removing it
GetEnumerator()Loop over the queue from front to back

Full list: C# Queue Methods index (/c-sharp/queue/method).

Frequently Asked Questions

It is a collection in System.Collections.Generic that stores items in first in, first out (FIFO) order. Enqueue adds at the back, Dequeue removes from the front, and Peek reads the front.
A List lets you read and change items by index and insert anywhere. A Queue only lets you add at the back and take from the front, which keeps the line fair and the code simple. A Queue has no indexer.
It throws InvalidOperationException. Check Count > 0 first, or use TryDequeue (newer .NET), which returns false instead of throwing.
No. If several threads add and remove items at the same time, use ConcurrentQueue<T> from System.Collections.Concurrent. The older non-generic System.Collections.Queue also exists, but prefer Queue<T> because it is type-safe.
Open the C# Queue Methods index. It lists Enqueue, Dequeue, TryDequeue, Clear, Peek, TryPeek, Contains, ToArray, CopyTo, GetEnumerator, EnsureCapacity, and TrimExcess.

Browse every Queue method

Open the methods index for categorized guides with examples and FAQs.

Queue Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful