C# Reference ยท Collections
The Stack<T> class
Reference for C# Stack<T>: Push, Pop, Peek, TryPop and TryPeek, empty-stack errors, enumeration order from the top, and uses such as undo and parsing.
Stack<T> is a last-in, first-out (LIFO) collection. Push places an item on top, Pop removes and returns the top item, and Peek looks at it without removing it. Think of a pile of plates: you can only add to or take from the top.
Stacks are the right tool for undo history, backtracking, depth-first search, evaluating expressions and matching nested brackets. Internally it is an array, so Push and Pop are O(1) amortized. Do not confuse the Stack<T> collection with the call stack or with stackalloc.
var history = new Stack<string>(); history.Push("page1"); history.Push("page2"); Console.WriteLine(history.Pop()); // page2
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public class Stack<T> : IEnumerable<T>, IReadOnlyCollection<T>, ICollection { public Stack(); public Stack(int capacity); public Stack(IEnumerable<T> collection); public int Count { get; } public void Push(T item); public T Pop(); public T Peek(); public bool TryPop(out T result); public bool TryPeek(out T result); public bool Contains(T item); public T[] ToArray(); public void Clear(); }
| Name | Description |
|---|---|
item |
The element placed on top. |
collection |
Items pushed in enumeration order: the last one ends on top. |
result (out) |
Receives the top item, or default(T) when the stack is empty. |
Returns: Pop removes and returns the top item; Peek returns it without removing it. TryPop and TryPeek return true when an item was available. ToArray returns the items top first.
Errors: Pop and Peek throw InvalidOperationException ("Stack empty.") on an empty stack. Modifying the stack during foreach throws InvalidOperationException.
Usage notes
- LIFO order: the most recently pushed item is the first popped. Use
Queue<T>for first-in, first-out. - Enumeration order is top to bottom, so
new Stack<char>("abc")enumerates asc,b,a.ToArray()gives the same order. - Constructing from a sequence pushes the items one by one, which reverses their order compared to the source.
- No index access. Only the top item is reachable directly.
Containsis O(n). - Recursion alternative: an explicit
Stack<T>turns a recursive algorithm (tree walks, flood fill) into a loop and avoidsStackOverflowException, which cannot be caught. - Prefer
TryPopwhen emptiness is normal, as in the bracket checker. - Not thread-safe. Use
ConcurrentStack<T>for shared use. - Capacity doubles when full. Pre-size with the
capacityconstructor if you know the depth. - Value types are copied into and out of the stack; a stack of large structs can be expensive.