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.
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.
Syntax
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 });
Reference
Queue vs List, Stack, and ConcurrentQueue
Pick the collection that matches how items should come out:
Type
Order of removal
Access
Good for
Queue<T>
First in, first out (oldest first)
Add at back, take from front, no indexer
Waiting lines, print jobs, tasks to process in arrival order
List<T>
Any item, by index
Read, insert, and remove anywhere
An ordered list you change in many places (see List)
Stack<T>
Last in, first out (newest first)
Push and pop at the top only
Undo history, going back; it is the opposite of a queue (see Stack)
ConcurrentQueue<T>
First in, first out
Thread-safe add and remove
A queue shared by several threads
Reference
Dequeue vs TryDequeue
Both take the item at the front. They differ when the queue might be empty:
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());
}
}
Output
3
Asha
Asha
2
Ben
How It Works
One type argument.Queue<string> means every item in the queue is a string. There are no keys, only a line of items.
Join at the back. Each Enqueue call adds a name behind the others, so after three calls Count is 3 and Asha is first.
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.
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");
}
}
}
Output
Serving Asha
Nobody is waiting
How It Works
The risk. Calling Dequeue() on an empty queue throws InvalidOperationException, so a bare call is only safe when you know someone is waiting.
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.
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.
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));
}
}
Output
Turn 1: Asha
Turn 2: Ben
Turn 3: Chitra
Turn 4: Asha
Turn 5: Ben
Chitra, Asha, Ben
How It Works
Start from an array. The constructor copies the names in order, so Asha is at the front and Chitra is at the back.
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).
A circle appears. Because nobody leaves for good, the order repeats: Asha, Ben, Chitra, then Asha and Ben again for turns 4 and 5.
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.
Continue
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.
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.