C# Stack Methods

Beginner
5 min read
Updated: Nov 2026
12 method guides
Method index

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.

Last in, first out.

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:

C#
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 methods

Put items on top, take the top item off, or empty the whole stack. TryPop does the removal without throwing on an empty stack.

MethodDescriptionTutorial
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 methods

See the top item without removing it, or ask whether an item is anywhere in the stack.

MethodDescriptionTutorial
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 methods

Copy the items, top to bottom, into a new array or into an array you already have.

MethodDescriptionTutorial
ToArray()Copy the stack into a new array, top to bottom. The stack itself is not changed.Try it
CopyTo()Copy the stack's items into an existing array, top first, starting at a position you choose.Try it

Loop

1 method

Walk through the stack from top to bottom without removing anything.

MethodDescriptionTutorial
GetEnumerator()Get an enumerator that walks the stack from top to bottom; foreach uses it for you.Try it

Capacity

2 methods

Reserve space before a big load, or give back unused space after a big clean-up.

MethodDescriptionTutorial
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

They are the methods on System.Collections.Generic.Stack<T>. They put items on top, take the top item off, look at the top, check for an item, copy the items to an array, loop through them, and manage capacity.
This index lists 12 guides: Push, Pop, TryPop, Clear, Peek, TryPeek, Contains, ToArray, CopyTo, GetEnumerator, EnsureCapacity, and TrimExcess.
Push, Pop, TryPop, and Clear change the items. EnsureCapacity and TrimExcess change only the hidden storage size. Peek, TryPeek, Contains, ToArray, CopyTo, and GetEnumerator only read the stack.
Last in, first out (LIFO). Pop, foreach, ToArray, and CopyTo all go from the top (the newest item) to the bottom (the oldest). If you push 1, 2, 3, a foreach prints 3, 2, 1.
Read the C# Stack hub first. Then open Push(), Pop(), Peek(), and TryPop() from the tables below.

Start with Push()

Stack up your first items: put them on top of a stack and see how the newest one always comes first.

Push() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful