C# Stack with Push & Pop Examples
⚡ Smart Summary
Stack in C# is a collection from the System.Collections namespace that follows the last in, first out (LIFO) principle, where the Push method adds an element to the top and the Pop method removes it.

What is Stack in C#?
The stack is a special case collection which represents a last in first out (LIFO) concept. To first understand LIFO, let’s take an example. Imagine a stack of books with each book kept on top of each other.
The concept of last in first out in the case of books means that only the top most book can be removed from the stack of books. It is not possible to remove a book from between, because then that would disturb the setting of the stack.
Hence in C#, the stack also works in the same way. Elements are added to the stack, one on the top of each other. The process of adding an element to the stack is called a push operation. To remove an element from a stack, you can also remove the top most element of the stack. This operation is known as pop.
Let’s look at the operations available for the Stack collection in more detail.
Declaration of the stack
A stack is created with the help of the Stack Data type. The keyword “new” is used to create an object of a Stack. The object is then assigned to the variable st.
Stack st = new Stack()
Adding elements to the stack
The push method is used to add an element onto the stack. The general syntax of the statement is given below.
Stack.push(element)
Removing elements from the stack
The pop method is used to remove an element from the stack. The pop operation will return the topmost element of the stack. The general syntax of the statement is given below
Stack.pop()
Count
This property is used to get the number of items in the Stack. Below is the general syntax of this statement.
Stack.Count
Contains
This method is used to see if an element is present in the Stack. Below is the general syntax of this statement. The statement will return true if the element exists, else it will return the value false.
Stack.Contains(element)
Now let’s see this working at a code level. All of the below-mentioned code will be written to our Console application. The code will be written to our Program.cs file.
In the below program, we will write the code to see how we can use the above-mentioned methods.
Example 1: Stack.Push() Method
In this example, we will see
- How a stack gets created.
- How to display the elements of the stack, and use the Count and Contain methods.
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push(1); st.Push(2); st.Push(3); foreach (Object obj in st) { Console.WriteLine(obj); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("The number of elements in the stack " +st.Count); Console.WriteLine("Does the stack contain the elements 3 "+st.Contains(3)); Console.ReadKey(); } } }
Code Explanation:-
- The first step is used to declare the Stack. Here we are declaring “st” as a variable to hold the elements of our stack.
- Next, we add 3 elements to our stack. Each element is added via the Push method.
- Now since the stack elements cannot be accessed via the index position like the array list, we need to use a different approach to display the elements of the stack. The Object (obj) is a temporary variable, which is declared for holding each element of the stack. We then use the foreach statement to go through each element of the stack. For each stack element, the value is assigned to the obj variable. We then use the Console.Writeline command to display the value to the console.
- We are using the Count property (st.count) to get the number of items in the stack. This property will return a number. We then display this value to the console.
- We then use the Contains method to see if the value of 3 is present in our stack. This will return either a true or false value. We then display this return value to the console.
If the above code is entered properly and the program is run the following output will be displayed.
Output:
From the output, we can see that the elements of the stack are displayed. Also, the value of True is displayed to say that the value of 3 is defined on the stack.
Note: You have noticed that the last element pushed onto the stack is displayed first. This is the topmost element of the stack. The count of stack elements is also shown in the output.
Example 2: Stack.Pop() Method
Now let’s look at the “remove” functionality. We will see the code required to remove the topmost element from the stack.
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push(1); st.Push(2); st.Push(3); st.Pop(); foreach (Object obj in st) { Console.WriteLine(obj); } Console.ReadKey(); } } }
Code Explanation:-
- Here we just issue the pop method which is used to remove an element from the stack.
If the above code is entered properly and the program is run, the following output will be displayed.
Output:
We can see that the element 3 was removed from the stack.
C# Stack Peek() Method
The Push and Pop methods work with the top of the stack, but sometimes you only need to look at the top element without removing it. The Peek method does exactly that. It returns the topmost value of the stack while leaving the collection unchanged, which is useful when you want to test the next item before deciding to pop it.
Step 1) Create a stack and push three elements onto it, so the value 3 sits on top.
Step 2) Call the Peek method to read the top element, then check the Count property to confirm nothing was removed.
using System; using System.Collections; namespace DemoApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push(1); st.Push(2); st.Push(3); Console.WriteLine(st.Peek()); Console.WriteLine(st.Count); Console.ReadKey(); } } }
Code Explanation:-
- A stack named st is declared, and the values 1, 2, and 3 are added with the Push method, so 3 becomes the topmost element.
- The Peek method returns the top element without removing it, and the Count property still reports the full number of items in the stack.
When the program runs, it prints the value 3 from Peek and then the count 3, which proves the element stays on the stack. If the stack is empty, Peek throws an InvalidOperationException, so it is safe to check Count before calling it.
Generic Stack<T> in C#
The examples above use the non-generic Stack class from System.Collections, which stores every element as an object. Modern C# code usually prefers the generic Stack<T> class from the System.Collections.Generic namespace. It binds the stack to a single declared type, so the compiler checks every Push and Pop and no casting or boxing is needed.
The key benefits of the generic Stack<T> are listed below.
- Type safety: The type is fixed at declaration, so only values of that type can be pushed, and errors are caught at compile time.
- No boxing: Value types such as int are stored directly, which avoids the boxing cost that the non-generic Stack pays.
- Cleaner reads: Pop and Peek return the declared type directly, so you do not cast the result back from object.
using System; using System.Collections.Generic; namespace DemoApplication { class Program { static void Main(string[] args) { Stack<string> st = new Stack<string>(); st.Push("Java"); st.Push("Python"); Console.WriteLine(st.Pop()); Console.ReadKey(); } } }
In this example the stack is declared as Stack<string>, so it accepts only string values. The Pop method returns a string directly and prints Python, the last value pushed. For any new project, the generic Stack<T> is the recommended choice.
Difference Between Stack and Queue in C#
A stack and a queue are both collections that control the order in which elements are removed, but they use opposite rules. A stack is last in, first out, while a queue is first in, first out. Choosing the right one makes the intent of your code clear to other developers.
The main differences are listed below:
- Order: A stack removes the most recently added element first (LIFO), while a queue removes the oldest element first (FIFO).
- Methods: A stack uses Push and Pop, whereas a queue uses Enqueue to add and Dequeue to remove.
- Top-element access: A stack reads its next item with Peek, and a queue reads its next item with Peek from the front.
- Typical uses: Stacks fit undo features, expression evaluation, and recursion tracking, while queues fit scheduling, buffering, and breadth-first traversal.
Both types are available in generic form as Stack<T> and Queue<T> in System.Collections.Generic, so you gain type safety with either choice.




