C# Stack Methods

About This Index
This page is the C# Stack Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. Examples use small fixed data such as Stack<int> and Stack<string>. A stack always walks its items in one fixed order (top to bottom), so the examples can print items directly and the output you see is the output you get.
Learn what a Stack is for on /c-sharp/stack. Use this page when you need a specific API such as Push(), Pop(), or TryPop(). If you need items to leave in arrival order instead, see Queue Methods.
A stack is a pile of plates. Push puts a plate on top, Pop takes the top plate off, and Peek looks at the top plate. Calling Pop or Peek on an empty stack throws, so check Count or use TryPop / TryPeek.
How to Call a Stack Method
Every Stack method is an instance method, so you call it on your stack variable:
Stack<string> pages = new Stack<string>();
pages.Push("home"); // put on top
pages.Push("about");
string current = pages.Peek(); // look at the top: "about"
string left = pages.Pop(); // take the top off: "about"
bool has = pages.Contains("home"); // search the whole stack
string[] copy = pages.ToArray(); // copy top to bottom
int depth = pages.Count; // 1 C# Stack Methods Table
All method guides in this C# tutorial series (12 tutorials), grouped by purpose.
Add / remove
4 methodsPut items on top, take the top item off, or empty the whole stack. TryPop does the removal without throwing on an empty stack.
| Method | Description | Tutorial |
|---|---|---|
Push() | Put an item on top of the stack, so it becomes the next one to come off. | Try it |
Pop() | Remove and return the item on top of the stack; throws if the stack is empty. | Try it |
TryPop() | Try to remove the top item; returns true or false instead of throwing on an empty stack. | Try it |
Clear() | Remove every item from the stack in one call; Count becomes 0. | Try it |
Look
3 methodsSee the top item without removing it, or ask whether an item is anywhere in the stack.
| Method | Description | Tutorial |
|---|---|---|
Peek() | Read the item on top of the stack without removing it; throws if the stack is empty. | Try it |
TryPeek() | Try to read the top item without removing it; returns false instead of throwing on an empty stack. | Try it |
Contains() | Check whether an item is anywhere in the stack; it scans the whole stack, so it is O(n). | Try it |
Copy / convert
2 methodsCopy the items, top to bottom, into a new array or into an array you already have.
Loop
1 methodWalk through the stack from top to bottom without removing anything.
| Method | Description | Tutorial |
|---|---|---|
GetEnumerator() | Get an enumerator that walks the stack from top to bottom; foreach uses it for you. | Try it |
Capacity
2 methodsReserve space before a big load, or give back unused space after a big clean-up.
| Method | Description | Tutorial |
|---|---|---|
EnsureCapacity() | Pre-size the stack so it can hold at least a number of items without growing again. | Try it |
TrimExcess() | Give back unused space after a stack shrinks; the items and their order stay the same. | Try it |
Frequently Asked Questions
Start with Push()
Stack up your first items: put them on top of a stack and see how the newest one always comes first.
12 people found this page helpful
