C# Stack with Examples
In C# a Stack is a Collection that follows the Last-In-First-Out (LIFO) principle. It is used when we need last-in, first-out access to items. In C# there are both generic and non-generic types of Stacks. The generic stack is in the System.Collections.Generic namespace, while the non-generic stack is in System.Collections. The stack can dynamically store elements of the same or different types.
Example: This example demonstrates the basic working of Stack.
// C# Program Implementing Stack Class
using System;
using System.Collections.Generic;
class Geeks
{
public static void Main(string[] args)
{
// Create a new stack
Stack<int> s = new Stack<int>();
// Push elements onto the stack
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
// Pop elements from the stack
while (s.Count > 0)
{
Console.WriteLine(s.Pop());
}
}
}
Output
4 3 2 1
Below is the diagramatic Representation of Working of Stack:

Hierarchy of Stack Class
The below diagram shows the Hierarchy of Stack class.

- The Stack class implements the IEnumerable, ICollection, and ICloneable interfaces.
- When we add an item to the list, it is called pushing the element.
- When we remove it, it is called popping the element.
- The capacity of a Stack is the number of elements the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.
- In Stack, we are allowed to store duplicate elements.
- A Stack accepts null as a valid value for reference types.
Creating a Stack
Stack class has three constructors which are used to create a stack which is as follows:
- Stack(): This constructor is used to create an instance of the Stack class which is empty and having the default initial capacity.
- Stack(ICollection): This constructor is used to create an instance of the Stack class which contains elements copied from the specified collection, and has the same initial capacity as the number of elements copied.
- Stack(Int32): This constructor is used to create an instance of the Stack class which is empty and having specified initial capacity or the default initial capacity, whichever is greater
Let’s see how to create a stack using Stack() constructor:
Step 1: Include System.Collections namespace in your program with the help of using keyword
using System.Collections;
Step 2: Create a Stack using Stack class
Stack s = new Stack();
Performing Various Operations on Stack
1. Adding Elements: We use push() method to insert elements in a stack.
Example: This example demonstrates how to create non-generic stack, add various elements to it and access them in Last-In-FIrst-Out (LIFO) Order.
// C# program to demonstrates how to
// create and add elements into a stack
using System;
using System.Collections;
class Geeks {
static public void Main()
{
// Create a stack
// Using Stack class
Stack s = new Stack();
// Adding elements in the Stack
// Using Push method
s.Push("Geek");
s.Push("geeksforgeeks");
s.Push(null);
s.Push(1);
s.Push(10.0);
// Accessing the elements
// of s Stack
// Using foreach loop
foreach(var elem in s) {
Console.WriteLine(elem);
}
}
}
Output
10 1 geeksforgeeks Geek
2. Removing Elements:The Stack class provides two different methods to remove elements and the methods are listed below:
- Clear() Method: The clear() is used to remove all the objects from the stack.
- Pop Method(): The pop() is used to removes the beginning element of the stack.
Example: This example displays the contents of the stack before and after removal.
// C# Program to remove elements
// from a stack
using System;
using System.Collections.Generic;
class Geeks {
public static void Main(string[] args)
{
// Initialize a stack
Stack<string> s = new Stack<string>();
// Inserting elements into the s using Push()
s.Push("Geeks");
s.Push("For");
s.Push("Geeks");
s.Push("For");
// Initial stack
Console.WriteLine("Initial stack: ");
foreach(var item in s) {
Console.WriteLine(item);
}
// Removing the top element
// from the stack
s.Pop();
// Final s after removal
Console.WriteLine("\nUpdated stack after Pop:");
foreach(var item in s) {
Console.WriteLine(item);
}
}
}
Output
Initial stack: For Geeks For Geeks Updated stack after Pop: Geeks For Geeks
3. Get the Topmost Element of the Stack: The Stack class provides two different methods to find the topmost element of the stack and the methods are listed below:
- Pop() Method: The Pop()method returns the object at the beginning of the stack with modification means this method removes the topmost element of the stack.
- Peek() Method: The Peek()method returns the object at the beginning of the stack without removing it.
Example: This example demonstrates how to peek at the topmost element of a Stack.
// C# Program to get the
// topmost element of the Stack
using System;
using System.Collections.Generic;
class Geeks {
public static void Main(string[] args)
{
// Create a new stack of integers
Stack<int> s1 = new Stack<int>();
// Push elements onto the stack
s1.Push(10);
s1.Push(20);
s1.Push(30);
// Checking if the stack is not empty before
// accessing the topmost element
if (s1.Count > 0) {
// Peek() returns the topmost element without
// removing it
int t = s1.Peek();
Console.WriteLine(
"The topmost element in the stack is: "
+ t);
}
else {
Console.WriteLine("The stack is empty.");
}
}
}
Output
The topmost element in the stack is: 30
4. Checking the Availability of Elements in the Stack: Stack class provide Contains() method to check if the element is present in the Stack or not.
Example:
// C# Program to to check the
// availability of elements in the stack
using System;
using System.Collections.Generic;
class Geeks {
public static void Main(string[] args)
{
// Create a new stack of integers
Stack<int> s = new Stack<int>();
// Push elements onto the stack
s.Push(10);
s.Push(20);
s.Push(30);
// Check if the element 20 is present in the stack
Console.WriteLine(
"The element 20 is present in the stack: "
+ s.Contains(20));
Console.WriteLine(
"The element 100 is present in the stack: "
+ s.Contains(100));
}
}
Output
The element 20 is present in the stack: True The element 100 is present in the stack: False
Generic Stack vs Non-Generic Stack
Generic Stack | Non-Generic Stack |
---|---|
Generic stack is defined under System.Collections.Generic namespace. | Non-Generic stack is defined under System.Collections namespace. |
Generic stack can only store same type of elements. | Non-Generic stack can store same type or different types of elements. |
There is a need to define the type of the elements in the stack | There is no need to define the type of the elements in the stack. |
It is type-safe. | It is not type-safe. |