In C#, Queue.Dequeue()removes the item at the front of a Queue<T> and returns it. This is the "first out" half of first in, first out (FIFO): the item that has waited longest leaves first.
Picture the person at the head of a ticket line walking up to the counter. They are served and leave the line, and the next person becomes the new front. If you only want to look at the front person without removing them, use Peek().
This is an instance member of Queue<T> (System.Collections.Generic). Call it on a queue variable, such as line.Dequeue(...). A Queue<T> is a class, so two variables can share one queue. It is first in, first out (FIFO): items leave in the order they arrived. It is not thread-safe; for several threads use ConcurrentQueue<T>.
Syntax
Call Dequeue with no arguments. It gives you back the removed item.
C#
Queue<string> line = new Queue<string>();
line.Enqueue("Asha");
line.Enqueue("Ben");
string served = line.Dequeue(); // "Asha"; the line is now just Ben
int left = line.Count; // 1
Call
What it does
line.Dequeue()
Removes the front item, returns it, and lowers Count by 1.
if (line.Count > 0) line.Dequeue();
Safe pattern: check that something is waiting before you take it.
line.TryDequeue(out string who)
No exception on an empty queue. Returns true or false instead (newer .NET). See TryDequeue().
line.Peek()
Look at the front item without removing it. See Peek().
Dequeue is O(1). It does not shift the other items; the queue just moves its front marker forward. Calling it on an empty queue throws InvalidOperationException, so check Count first or use TryDequeue. The non-generic System.Collections.Queue also has a Dequeue, but prefer Queue<T> because it is type-safe.
Library declarations
C#
public T Dequeue();
Parameters
Parameter
Description
None
Dequeue() takes no parameters.
Return Value
Return Value
Description
T
The item that was at the front of the queue. That item is no longer in the queue after the call.
C# Examples
Example 1 — Serve the First Person in Line
Dequeue one name, see who it was, and check who is at the front now.
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");
string served = line.Dequeue();
Console.WriteLine(served);
Console.WriteLine(line.Count);
Console.WriteLine(line.Peek());
}
}
Output
Asha
2
Ben
How It Works
Asha joined first. She is at the front, so Dequeue() hands her name back and takes her out of the queue.
Return value kept. The removed name is stored in served and printed. If you ignore the return value, the item is simply gone.
Count drops. Three people were waiting and one left, so Count prints 2.
New front. Ben is now the first in line, and Peek() shows him without removing him.
Example 2 — Guard Against an Empty Queue
Dequeue the only item, then try again. First use a Count check, then see what happens without one.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> tickets = new Queue<int>();
tickets.Enqueue(101);
Console.WriteLine(tickets.Dequeue());
if (tickets.Count > 0) {
Console.WriteLine(tickets.Dequeue());
} else {
Console.WriteLine("Nobody is waiting");
}
try {
tickets.Dequeue();
} catch (InvalidOperationException ex) {
Console.WriteLine(ex.GetType().Name);
}
}
}
Output
101
Nobody is waiting
InvalidOperationException
How It Works
One ticket only. The queue holds just ticket 101, so the first Dequeue() prints it and leaves the queue empty.
Check first.tickets.Count > 0 is now false, so the code takes the else branch and the unsafe call never runs.
No guard, no mercy. The last block calls Dequeue() on the empty queue anyway. There is no item to return, so .NET throws an exception.
Type name only. The catch block prints ex.GetType().Name, which is always InvalidOperationException. The longer message text can change between versions, so it is not printed.
Example 3 — Serve Everyone Until the Queue Is Empty
Build a queue from an array, then loop with Count as the guard and add up the token numbers as they are served.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> tokens = new Queue<int>(new int[] { 21, 22, 23, 24 });
int sum = 0;
while (tokens.Count > 0) {
int token = tokens.Dequeue();
Console.WriteLine("Serving token " + token);
sum += token;
}
Console.WriteLine(sum);
Console.WriteLine(tokens.Count);
}
}
Array to queue. The constructor copies the array into the queue in order, so token 21 (index 0) is at the front and 24 is at the back.
Loop guard.while (tokens.Count > 0) stops the loop before Dequeue() could ever see an empty queue.
Same order as the array. Each turn removes the current front, so the tokens are served 21, 22, 23, 24: the order they were stored.
Totals. The running sum is 21 + 22 + 23 + 24 = 90, and when the loop ends Count is 0.
Frequently Asked Questions
It removes the item at the front of the queue and returns it. The removed item is no longer in the queue, and Count goes down by 1.
It throws InvalidOperationException. Check Count > 0 before you call it, or use TryDequeue, which returns false instead of throwing.
Dequeue removes the front item and returns it. Peek only returns the front item and leaves it in the queue.
The first one: the item that has been waiting the longest. A queue is first in, first out. If you want the newest item, you need a Stack<T> instead.
Use while (queue.Count > 0) { var item = queue.Dequeue(); ... }. Do not use foreach to remove items, because changing a queue while foreach is running throws an exception.