Using Queue<T>.TryPeek in C# to Inspect Items Safely
Learn how Queue<T>.TryPeek works in C#, how it differs from Peek, and how to safely inspect queue items without removing them.

When you need to look at the first item in a Queue<T> without removing it, Peek is the usual choice. But Peek throws an InvalidOperationException if the queue is empty. In production, a queue may be empty at the moment you inspect it, and an exception is often the wrong way to handle that normal condition. TryPeek, introduced in .NET Core 2.0, solves this by returning a boolean instead of throwing. This article explains how Queue<T>.TryPeek works, how it differs from Peek, and where it fits in real-world code.
Why TryPeek Exists
The Queue<T> class has had a Peek method since the beginning. It returns the element at the beginning of the queue without removing it. The problem is that Peek throws an exception when the queue contains no elements. That forces you to either check Count first or catch the exception. Both approaches add noise and can hide logic errors. TryPeek follows the same pattern as TryGetValue in dictionaries: it returns a bool indicating success and provides the value through an out parameter. This makes the empty case a normal condition rather than an exceptional one.
TryPeek Syntax and Return Value
TryPeek is a method on Queue<T> and uses an out parameter instead of returning the item directly. The signature used in typical application code is:
public bool TryPeek(out T result)The method returns true if the queue is not empty and sets result to the first element. If the queue is empty, it returns false and sets result to the default value of T (null for reference types, zero for numeric types, and so on). Here is a minimal example:
Queue<string> queue = new Queue<string>();
queue.Enqueue("first");
queue.Enqueue("second");
if (queue.TryPeek(out string item))
{
Console.WriteLine($"First item: {item}");
}
else
{
Console.WriteLine("Queue is empty");
}Because the out parameter is assigned even when the method returns false, you must check the return value before using item. Otherwise, you may treat null or zero as if it were a real queued value.
Using TryPeek with a Queue of Reference Types
When T is a reference type, the out parameter receives null on an empty queue. This can be convenient if you want to treat an empty queue as a null result, but it also means you cannot distinguish between a queue that contains a null element and an empty queue. For example, if you enqueue a null string, TryPeek will return true and set result to null. That is correct behavior, but it can be confusing if you are not careful. The following code shows the distinction:
Queue<string?> queue = new Queue<string?>();
queue.Enqueue(null);
if (queue.TryPeek(out string? item))
{
// item is null, but the queue is not empty
}
else
{
// queue is empty
}If you need to distinguish between an empty queue and a queue containing a null value, check Count separately or use a sentinel value. In most cases, the boolean return is sufficient because you only care about whether there is an element to inspect.
TryPeek vs Peek: When to Use Which
Peek and TryPeek serve the same purpose, but they handle the empty case differently. Peek throws an exception; TryPeek returns a bool. The choice depends on how you expect the queue to behave in your application.
| Scenario | Recommended method | Reason |
|---|---|---|
| Queue is guaranteed non-empty by logic | Peek | Throwing an exception on empty indicates a programming error |
| Queue may be empty during normal operation | TryPeek | Avoids exception overhead and simplifies control flow |
You want to avoid an extra Count check | TryPeek | Combines the check and the retrieval in one call |
| You are writing a library that accepts a queue from callers | TryPeek | Lets the caller decide how to handle an empty queue |
If you use Peek and the queue is empty, you get an InvalidOperationException. That exception is expensive to generate and can be difficult to debug if it happens deep inside a loop. TryPeek makes the empty case a first-class result, which often leads to cleaner code. For example, consider a worker that processes messages from a queue:
while (true)
{
if (queue.TryPeek(out Message? next))
{
Process(next);
queue.Dequeue();
}
else
{
// No work available; wait or break
break;
}
}With Peek, you would need to check Count before calling Peek, which is two operations instead of one. The TryPeek pattern is also consistent with other .NET collection methods like Dictionary.TryGetValue.
Thread Safety and Concurrent Access
Queue<T> is not thread-safe. If multiple threads access the same queue concurrently, you must synchronize access yourself. TryPeek does not change that requirement. It is not atomic with Dequeue. If one thread calls TryPeek while another thread calls Dequeue, you can get a false return even though an item was enqueued just before, or you can get a true return and then the item is removed by the other thread before you process it. In a multithreaded environment, use a lock or a concurrent collection.
For a single-producer, single-consumer pattern, you might use a lock around both TryPeek and Dequeue to ensure consistency. For a more scalable approach, consider System.Collections.Concurrent.ConcurrentQueue<T>. It provides a TryPeek method that is thread-safe and does not require external locking. However, ConcurrentQueue<T>.TryPeek still does not guarantee that the item will remain in the queue after the call. It only tells you what is at the head at that moment. If you need to inspect and then remove an item atomically, use TryDequeue instead.
Performance Considerations
TryPeek is an O(1) operation. It reads the element at the head of the queue and returns it without allocating or copying. The only extra work is the branch that checks whether the queue is empty. In a non-empty queue, TryPeek performs the same head read as Peek; when the queue is empty, it avoids the exception that Peek would throw. Exceptions are expensive because they involve stack unwinding and often logging. If a loop frequently checks an empty queue, using TryPeek instead of Peek inside a try-catch can avoid repeated exception overhead.
TryPeek also returns the default value of T on an empty queue. For a reference type, that default is null; for a value type, it is the type's default value. Because the method is on a generic type, returning that default does not introduce boxing in ordinary generic code.
Common Mistakes and Edge Cases
One common mistake is to call TryPeek and then use the out parameter without checking the return value. This can lead to using a default value when the queue is empty. For example, if you peek at a queue of integers and the queue is empty, item will be 0. If you process that 0 as a real value, you have introduced a logic error. Always check the boolean result.
Another edge case is when the queue contains a null reference. As mentioned earlier, TryPeek returns true and sets the out parameter to null. If your code assumes that a null out parameter means an empty queue, you will misbehave. Use the return value as the source of truth, not the nullness of the out parameter.
Finally, remember that TryPeek does not modify the queue. It only reads the head. If you need to remove the item after inspecting it, you must call Dequeue separately. The pattern of TryPeek followed by Dequeue is not atomic. In a single-threaded context, it is fine, but in a concurrent context, use TryDequeue to get the item and remove it in one step.
For a queue that is shared across threads, the safest approach is to use ConcurrentQueue<T> and its TryDequeue method. If you must use Queue<T> with locking, make sure the lock covers both the peek and the dequeue operations so that no other thread can modify the queue between them.
Understanding these details helps you use TryPeek correctly and avoid the pitfalls that come with inspecting a queue without removing an element.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.