In C#, Queue.Peek()returns the item at the front of a Queue<T> but does not remove it. The queue stays exactly as it was; Count does not change.
It is like glancing at the first person in a ticket line to see who is next, without calling them to the counter yet. When you actually want to take that person out of the line, use Dequeue(). A queue only lets you see the front; there is no way to peek at the back or the middle.
This is an instance member of Queue<T> (System.Collections.Generic). Call it on a queue variable, such as line.Peek(...). 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 Peek with no arguments. It returns the front item.
C#
Queue<string> line = new Queue<string>();
line.Enqueue("Asha");
line.Enqueue("Ben");
string next = line.Peek(); // "Asha"; she is still in the queue
int total = line.Count; // 2
Call
What it does
line.Peek()
Returns the front item. The queue is not changed.
if (line.Count > 0) line.Peek();
Safe pattern: look only when something is waiting.
line.TryPeek(out string front)
No exception on an empty queue; returns true or false (newer .NET). See TryPeek().
Peek is O(1). On an empty queue it throws InvalidOperationException, just like Dequeue. If T is a class, the returned reference points to the same object that stays inside the queue, so changing that object changes the queued item too.
Library declarations
C#
public T Peek();
Parameters
Parameter
Description
None
Peek() takes no parameters.
Return Value
Return Value
Description
T
The item at the front of the queue. It stays in the queue.
C# Examples
Example 1 — See Who Is Next Without Removing Them
Peek twice and check that the answer and the Count are the same both times.
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.Peek());
Console.WriteLine(line.Peek());
Console.WriteLine(line.Count);
}
}
Output
Asha
Asha
3
How It Works
Front means oldest. Asha joined first, so she is the item Peek() reports.
Asking twice, same answer. Nothing was removed by the first call, so the second call prints Asha again.
Count unchanged. All three people are still in the queue, so Count is 3.
Read-only. Use Peek when you need to decide something about the next item before you commit to removing it.
Example 2 — Peek Safely When the Queue Might Be Empty
Check Count before peeking, then see the exception that happens when you skip the check.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> numbers = new Queue<int>();
if (numbers.Count > 0) {
Console.WriteLine(numbers.Peek());
} else {
Console.WriteLine("Empty, nothing to peek");
}
numbers.Enqueue(42);
Console.WriteLine(numbers.Peek());
numbers.Dequeue();
try {
Console.WriteLine(numbers.Peek());
} catch (InvalidOperationException ex) {
Console.WriteLine(ex.GetType().Name);
}
}
}
Output
Empty, nothing to peek
42
InvalidOperationException
How It Works
Guard first. A new queue has Count 0, so the if test fails and the else text is printed. Peek() is never called.
One item. After Enqueue(42), the front exists, so Peek() prints 42.
Empty again.Dequeue() removes the 42, and the queue has no front item anymore.
Unguarded call. This Peek() has nothing to return, so it throws. The catch block prints only the exception type name, which is stable across .NET versions.
Example 3 — Enqueue Does Not Change the Front
Watch the front item while you add new items at the back, and see when it finally changes.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> queue = new Queue<int>();
queue.Enqueue(10);
Console.WriteLine("Front: " + queue.Peek());
queue.Enqueue(20);
queue.Enqueue(30);
Console.WriteLine("Front: " + queue.Peek());
queue.Dequeue();
Console.WriteLine("Front: " + queue.Peek());
queue.Dequeue();
Console.WriteLine("Front: " + queue.Peek());
}
}
Output
Front: 10
Front: 10
Front: 20
Front: 30
How It Works
First item is the front. With a single item, 10 is both the first and the last, so it is the front.
Newcomers wait. Adding 20 and 30 puts them behind 10. The line shows Front: 10 a second time.
The front moves only on removal. The first Dequeue() takes 10 away, so 20 steps up to the front.
Last step. A second Dequeue() removes 20, and the oldest remaining item is 30. This is first in, first out in action.
Frequently Asked Questions
It returns the item at the front of the queue without removing it. The queue and its Count stay the same.
It throws InvalidOperationException. Check Count > 0 first, or use TryPeek, which returns false instead of throwing.
Peek only looks at the front item. Dequeue returns the front item and also removes it from the queue.
Not directly. A queue only exposes its front. If you must see the last item, call ToArray() and read the final element, but that copies the whole queue, so use it rarely.
No. Peek never changes the queue in any way. It does not even count as a modification, so it is safe to call while you loop with foreach.