In C#, Queue.TryDequeue(out result) is the safe version of Dequeue(). If an item is waiting, it removes the front item, hands it to you in the out variable, and returns true. If the queue is empty, it returns false and does not throw.
Think of a counter clerk who calls "Next, please!" If the line is empty, the clerk simply says "nobody here" instead of falling over. This is handy in loops and in code where an empty queue is normal, not an error.
This is an instance member of Queue<T> (System.Collections.Generic). Call it on a queue variable, such as line.TryDequeue(...). 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>. TryDequeue exists in newer .NET versions (.NET Core 2.0 and later, and .NET Standard 2.1). It is not in classic .NET Framework, so check your project version.
Syntax
Pass an out variable. Read the bool result first, and only then use the variable.
C#
Queue<string> line = new Queue<string>();
line.Enqueue("Asha");
if (line.TryDequeue(out string who)) {
// who is "Asha" here
}
bool again = line.TryDequeue(out string nobody); // false; nobody is null
Call
What it does
line.TryDequeue(out string who)
If the queue has items: removes the front, stores it in who, returns true.
line.TryDequeue(out string who) (empty)
Returns false. who gets the default value (null for strings, 0 for int).
while (line.TryDequeue(out var x)) { ... }
A neat loop that serves everyone and stops by itself when the queue is empty.
Use the bool result to decide, not the value. A queue of int may hold a real 0, and an empty queue also gives 0, so only the true / false tells them apart. Like Dequeue, it is O(1). In a multi-threaded program use ConcurrentQueue<T>.TryDequeue, because a plain Queue<T> is not thread-safe.
Library declarations
C#
public bool TryDequeue(out T result);
Parameters
Parameter
Description
result
An out parameter. When the method returns true, it holds the removed front item. When it returns false, it holds the default value of T.
Return Value
Return Value
Description
bool
true if an item was removed from the front. false if the queue was empty (nothing changed).
C# Examples
Example 1 — Dequeue Safely and Read the Result
Dequeue the only name, then try again on the now-empty queue. Compare the bool and the out value each time.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<string> line = new Queue<string>();
line.Enqueue("Asha");
bool ok = line.TryDequeue(out string name);
Console.WriteLine(ok);
Console.WriteLine(name);
ok = line.TryDequeue(out name);
Console.WriteLine(ok);
Console.WriteLine(name == null);
}
}
Output
True
Asha
False
True
How It Works
First call succeeds. One name is waiting, so it is removed, name becomes "Asha", and the method returns True.
Declared on the spot.out string name creates the variable inside the call, so there is no separate declaration line.
Second call fails quietly. The queue is empty now. Instead of throwing, the method returns False.
Default value. On failure, name is reset to the default for string, which is null, so name == null prints True.
Example 2 — A Loop That Stops by Itself
Use TryDequeue as the loop condition. No Count check and no try/catch are needed.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> numbers = new Queue<int>();
numbers.Enqueue(4);
numbers.Enqueue(8);
numbers.Enqueue(15);
int total = 0;
while (numbers.TryDequeue(out int n)) {
Console.WriteLine("Got " + n);
total += n;
}
Console.WriteLine(total);
Console.WriteLine(numbers.Count);
}
}
Output
Got 4
Got 8
Got 15
27
0
How It Works
The call is the condition. Each time the while line runs, it tries to remove one item and also tests whether that worked.
Item in n. While the result is true, n holds the item that was just removed, so the body can use it directly.
Natural stop. After 15 is taken, the next attempt finds nothing and returns false, and the loop ends without any extra code.
Summary. The total is 4 + 8 + 15 = 27, and the queue ends empty with Count 0.
Example 3 — Why You Must Check the Bool
Put a real 0 in a queue of numbers. Then try an empty queue. The out value is 0 both times, only the bool differs.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> scores = new Queue<int>();
scores.Enqueue(0);
bool first = scores.TryDequeue(out int a);
Console.WriteLine(first + " " + a);
bool second = scores.TryDequeue(out int b);
Console.WriteLine(second + " " + b);
}
}
Output
True 0
False 0
How It Works
A real zero. The queue stored the number 0 as a genuine item. Removing it succeeds, so the line shows True 0.
An empty queue. The next call has nothing to remove. It returns False and sets b to the default for int, which is also 0.
Same number, different meaning. Both lines end with 0, but only the first one is real data. Testing b == 0 would give a wrong answer.
Rule of thumb. Always branch on the returned bool, and read the out variable only when it is true.
Frequently Asked Questions
It tries to remove the front item. If the queue has an item, it returns true and gives the item in the out parameter. If the queue is empty, it returns false and does not throw.
Dequeue throws InvalidOperationException on an empty queue. TryDequeue returns false instead. Use TryDequeue when an empty queue is a normal situation.
It has the default value of the item type, such as null for string or 0 for int. Do not treat it as real data.
It is in newer .NET versions: .NET Core 2.0 and later and .NET Standard 2.1. It is not in classic .NET Framework. If your project does not compile with it, use Count with Dequeue.
No. Queue<T> is not thread-safe. For several threads, use ConcurrentQueue<T>, which has its own TryDequeue that is safe to call at the same time.