In C#, Stack.Peek()returns the item on top of a Stack<T> but does not remove it. The stack stays exactly as it was; Count does not change.
It is like glancing at the top plate to see its color before you decide to lift it. When you actually want to take it off, use Pop(). A stack lets you see only the top; there is no way to peek at the bottom or the middle.
This is an instance member of Stack<T> (System.Collections.Generic). Call it on a stack variable, such as history.Peek(...). A Stack<T> is a class, so two variables can share one stack. It is last in, first out (LIFO): the newest item sits on top and leaves first. It is not thread-safe; for several threads use ConcurrentStack<T>.
Syntax
Call Peek with no arguments. It returns the top item.
C#
Stack<string> plates = new Stack<string>();
plates.Push("white");
plates.Push("blue");
string top = plates.Peek(); // "blue"; it is still on the stack
int total = plates.Count; // 2
Call
What it does
plates.Peek()
Returns the top item. The stack is not changed.
if (plates.Count > 0) plates.Peek();
Safe pattern: look only when something is on the pile.
plates.TryPeek(out string top)
No exception on an empty stack; returns true or false (newer .NET). See TryPeek().
Peek is O(1). On an empty stack it throws InvalidOperationException, just like Pop. If T is a class, the returned reference points to the same object that stays in the stack, so changing that object changes the stacked item too.
Library declarations
C#
public T Peek();
Parameters
Parameter
Description
None
Peek() takes no parameters.
Return Value
Return Value
Description
T
The item on top of the stack. It stays in the stack.
C# Examples
Example 1 — See the Top Plate Without Lifting It
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() {
Stack<string> plates = new Stack<string>();
plates.Push("white");
plates.Push("blue");
plates.Push("red");
Console.WriteLine(plates.Peek());
Console.WriteLine(plates.Peek());
Console.WriteLine(plates.Count);
}
}
Output
red
red
3
How It Works
Top means newest. Red was pushed last, so it is the item Peek() reports. White, at the bottom, is not visible.
Same answer twice. The first call removed nothing, so the second call prints red again.
Count is unchanged. All three plates are still stacked, so Count is 3.
Read-only. Use Peek when you need to decide something about the top item before you commit to removing it.
Example 2 — Peek Safely When the Stack 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() {
Stack<string> pages = new Stack<string>();
if (pages.Count > 0) {
Console.WriteLine(pages.Peek());
} else {
Console.WriteLine("No page open");
}
pages.Push("home");
Console.WriteLine(pages.Peek());
pages.Pop();
try {
Console.WriteLine(pages.Peek());
} catch (InvalidOperationException ex) {
Console.WriteLine(ex.GetType().Name);
}
}
}
Output
No page open
home
InvalidOperationException
How It Works
Guard first. A new stack has Count 0, so the if test fails and No page open is printed. Peek() is never called.
One page. After Push("home") a top item exists, so Peek() prints home.
Empty again.Pop() takes the page off, and the stack has no top item any more.
No guard this time. This Peek() has nothing to return, so it throws. The catch block prints only the exception type name, which stays stable across .NET versions.
Example 3 — The Top Changes Only on Push or Pop
Watch the top item as you push new values and pop them again, and see exactly when it changes.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Stack<int> stack = new Stack<int>();
stack.Push(10);
Console.WriteLine("Top: " + stack.Peek());
stack.Push(20);
stack.Push(30);
Console.WriteLine("Top: " + stack.Peek());
stack.Pop();
Console.WriteLine("Top: " + stack.Peek());
stack.Pop();
Console.WriteLine("Top: " + stack.Peek());
}
}
Output
Top: 10
Top: 30
Top: 20
Top: 10
How It Works
One item. With only 10 in the stack, it is the top.
New items take over. Pushing 20 and then 30 covers 10, so the top jumps to 30. Newer always wins here, the opposite of a line.
Popping uncovers. The first Pop() removes 30, and 20 is visible again.
Back to the start. A second Pop() removes 20, and the 10 pushed first is on top again. Last in, first out.
Frequently Asked Questions
It returns the item on top of the stack without removing it. The stack 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 top item. Pop returns the top item and also removes it from the stack.
Not directly. A stack only exposes its top. If you must see the bottom item, call ToArray() and read the last element, but that copies the whole stack, so use it rarely.
No. Peek never changes the stack in any way. It does not even count as a modification, so it is safe to call while you loop with foreach.