In C#, Stack.Pop()removes the item on top of a Stack<T> and returns it. This is the "last in" half turned around: the newest item is the first one to leave.
Picture lifting the top plate off a pile. The plate underneath is now the top one. If you only want to look at the top plate without lifting it, use Peek(). Undo buttons and browser Back buttons work this way: the most recent step is taken off first.
This is an instance member of Stack<T> (System.Collections.Generic). Call it on a stack variable, such as history.Pop(...). 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 Pop with no arguments. It gives you back the removed item.
C#
Stack<string> plates = new Stack<string>();
plates.Push("white");
plates.Push("blue");
string taken = plates.Pop(); // "blue"; only white is left
int left = plates.Count; // 1
Call
What it does
plates.Pop()
Removes the top item, returns it, and lowers Count by 1.
if (plates.Count > 0) plates.Pop();
Safe pattern: check that something is on the pile before you take it.
plates.TryPop(out string top)
No exception on an empty stack. Returns true or false instead (newer .NET). See TryPop().
plates.Peek()
Look at the top item without removing it. See Peek().
Pop is O(1). It does not shift the other items; the stack just lowers its top marker by one. Calling it on an empty stack throws InvalidOperationException, so check Count first or use TryPop. The non-generic System.Collections.Stack also has a Pop, but prefer Stack<T> because it is type-safe.
Library declarations
C#
public T Pop();
Parameters
Parameter
Description
None
Pop() takes no parameters.
Return Value
Return Value
Description
T
The item that was on top of the stack. That item is no longer in the stack after the call.
C# Examples
Example 1 — Take the Top Item Off
Pop one name, see which one it was, and check what is on top afterwards.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Stack<string> names = new Stack<string>();
names.Push("Asha");
names.Push("Ben");
names.Push("Chitra");
string taken = names.Pop();
Console.WriteLine(taken);
Console.WriteLine(names.Count);
Console.WriteLine(names.Peek());
}
}
Output
Chitra
2
Ben
How It Works
Chitra went on last. She is on top, so Pop() hands her name back and takes her off the stack. Asha, who arrived first, stays at the bottom.
Keep the return value. The removed name goes into taken and is printed. If you ignore it, the item is simply gone.
Count drops. Three items were stacked and one left, so Count prints 2.
A new top. Ben was right under Chitra, so he is now the top, and Peek() shows him without removing him.
Example 2 — Guard Against an Empty Stack
Pop 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() {
Stack<int> tickets = new Stack<int>();
tickets.Push(101);
Console.WriteLine(tickets.Pop());
if (tickets.Count > 0) {
Console.WriteLine(tickets.Pop());
} else {
Console.WriteLine("Nothing to pop");
}
try {
tickets.Pop();
} catch (InvalidOperationException ex) {
Console.WriteLine(ex.GetType().Name);
}
}
}
Output
101
Nothing to pop
InvalidOperationException
How It Works
A single ticket. The stack holds only 101, so the first Pop() prints it and leaves the stack empty.
Look before you pop.tickets.Count > 0 is now false, so the else branch runs and the risky call is never made.
Skipping the guard. The last block pops from the empty stack anyway. There is nothing to hand back, so .NET raises an exception.
Print the type only. The catch block writes ex.GetType().Name, which is always InvalidOperationException. The message text may differ between versions, so it is left out.
Example 3 — Build a Back Button
A browser remembers each page on a stack. Going back means popping the current page and showing the one below it.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Stack<string> history = new Stack<string>();
history.Push("home");
history.Push("products");
history.Push("cart");
while (history.Count > 1) {
string left = history.Pop();
Console.WriteLine("Left " + left + ", now on " + history.Peek());
}
Console.WriteLine(history.Count);
}
}
Output
Left cart, now on products
Left products, now on home
1
How It Works
Visit order. Home was opened first and cart last, so cart is on top and is the current page.
Stop at one page. The loop runs while Count is above 1, so it never pops the last page and never pops from an empty stack.
Pop, then Peek. Each turn pops the current page, then Peek() reports the page that was underneath it, which is where the user lands.
The first page stays. After two Back clicks only home is left, so the last line prints 1.
Frequently Asked Questions
It removes the item on top of the stack and returns it. The removed item is no longer in the stack, and Count goes down by 1.
It throws InvalidOperationException. Check Count > 0 before you call it, or use TryPop, which returns false instead of throwing.
Pop removes the top item and returns it. Peek only returns the top item and leaves it on the stack.
The last one: the newest item on top. A stack is last in, first out. If you want the oldest item, you need a Queue<T> instead.
Use while (stack.Count > 0) { var item = stack.Pop(); ... }. Do not use foreach to remove items, because changing a stack while foreach is running throws an exception.