10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Stack Collection in C#

Updated on Sep 3, 2025
 
45,995 Views

The Stack Collection is used when the data items need to be arranged in a LIFO (Last In First Out) manner. When a data item is entered into the collection, it is called push. When the data item is removed from the collection, it is called pop.

Constructors in Stack Collection

The different constructors and their description is given as follows:

Constructors

Description

Stack()

This constructor initializes a new instance of the Stack class that is empty and has the default initial capacity.

Stack(ICollection)

This constructor initializes a new instance of the Stack class that contains elements copied from the specified collection and has the same initial capacity as the number of elements copied

Stack(Int32)

This constructor initializes a new instance of the Stack class that is empty and has the specified initial capacity or the default initial capacity, whichever is greater.

Table: Constructors in Stack Collection in C#
Source: MSDN

Properties in Stack Collection

The different properties and their description is given as follows:

Properties

Description

Count

This property obtains the number of elements contained in the Stack.

IsSynchronized

This property gets a value indicating whether access to the Stack is synchronized (thread safe).

SyncRoot

This property gets an object that can be used to synchronize access to the Stack.

Table: Properties in Stack Collection in C#
Source: MSDN

Methods in Stack Collection

The different methods and their description is given as follows:

Methods

Description

Clear()

This method removes all the objects from the Stack.

Clone()

This method creates a shallow copy of the Stack.

Contains(Object)

This method determines whether an element is in the Stack or not.

CopyTo(Array, Int32)

This method copies the Stack to an existing one-dimensional Array, starting at the specified array index.

Equals(Object)

This method determines whether the specified object is equal to the current object.

GetEnumerator()

This method returns an IEnumerator for the Stack.

GetHashCode()

This method serves as the default hash function.

GetType()

This method gets the Type of the current instance.

Peek()

This method returns the object at the top of the Stack without removing it.

Pop()

This method removes and returns the object at the top of the Stack.

Push(Object)

This method inserts an object at the top of the Stack.

Synchronized(Stack)

This method returns a synchronized (thread safe) wrapper for the Stack.

ToArray()

This method copies the Stack to a new array.

ToString()

This method returns a string that represents the current object.

Table: Methods in Stack Collection in C#
Source: MSDN

Let us now see an example, wherein we are adding and removing elements from a stack:

using System;
using System.Collections;
namespace StackExample {
  class Example {
     static void Main(string[] args) {
        Stack st = new Stack();
        st.Push('w');
        st.Push('x');
        st.Push('y');
        Console.WriteLine("Stack = ");
        foreach (char ch in st) {
           Console.Write(ch + " ");
        }
        Console.WriteLine();
        st.Push('s');
        st.Push('t');
        st.Push('u');
        st.Push('v');
        Console.WriteLine("Stack after adding two elements... ");
        foreach (char ch in st) {
           Console.Write(ch + " ");
        }
        Console.WriteLine();
        Console.WriteLine("Pop a value!");
        st.Pop();
        Console.WriteLine("Current stack:");
        foreach (char c in st) {
           Console.Write(c + " ");
        }
        Console.WriteLine("\nPop another value!");
        st.Pop();
        Console.WriteLine("Current stack:");
        foreach (char c in st) {
           Console.Write(c + " ");
        }
     }
  }
}

The output of the above program is given below:

Stack =
y x w
Stack after adding two elements...
v u t s y x w
Pop a value!
Current stack:
u t s y x w
Pop another value!
Current stack:
t s y x w

Stack Operations

Some of the stack operations are given as follows:

Push an element into Stack

An element is pushed into the stack using the Push() method. The program that demonstrates this is given as follows:

using System;
using System.Collections;
namespace StackDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Stack s = new Stack();
        s.Push(1);
        s.Push(2);
        s.Push(3);
        s.Push(4);
        Console.WriteLine("Stack Elements are: ");
        foreach (int i in s)
        {
           Console.WriteLine(i);
        }
     }
  }
}

Source Code: Program to push elements in Stack in C#

The output of the above program is given below:

Stack Elements are:
4
3
2
1

Pop an element from Stack

The top element can be popped from the stack using the Pop() method. The program that demonstrates this is given as follows:

using System;
using System.Collections;
namespace StackDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Stack s = new Stack();
        s.Push(1);
        s.Push(2);
        s.Push(3);
        s.Push(4);
        Console.WriteLine("Original Stack: ");
        foreach (int i in s)
        {
           Console.WriteLine(i);
        }
        s.Pop();
        s.Pop();
        Console.WriteLine("Stack after Pop() operation: ");
        foreach (int i in s)
        {
           Console.WriteLine(i);
        }
     }
  }
}

Source Code: Program to pop elements from Stack in C#

The output of the above program is given below:

Original Stack:
4
3
2
1
Stack after Pop() operation:
2
1

Peek into the stack

The element at the top of the stack can be viewed without removing it using the Peek() method. The program that demonstrates this is given as follows:

using System;
using System.Collections;
namespace StackDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Stack s = new Stack();
        s.Push(1);
        s.Push(2);
        s.Push(3);
        s.Push(4);
        Console.WriteLine("Stack elements are: ");
        foreach (int i in s)
        {
           Console.WriteLine(i);
        }
        Console.WriteLine("Element at top is {0}", s.Peek());
     }
  }
}

Source Code: Program to peek top element in Stack in C#

The output of the above program is given below:

Stack elements are:
4
3
2
1
Element at top is 4

Reverse a stack

Let us see how we can reverse a stack in C#:

using System;
using System.Collections;
namespace StackExample {
public class Example {
  public static void Main(string[] args) {
  Stack currentStack = new Stack();
  Stack revStack = new Stack();
  currentStack.Push('A');
  currentStack.Push('B');
  currentStack.Push('C');
  currentStack.Push('D');
  currentStack.Push('E');
  currentStack.Push('F');
  Console.WriteLine("Current stack... ");
  foreach(char ch in currentStack) {
   Console.Write(ch + "\n");
  }
  while (currentStack.Count != 0) {
   revStack.Push(currentStack.Pop());
  }
  Console.WriteLine("\nReversed stack... ");
  foreach(char cH in revStack) {
   Console.Write(cH + "\n");
  }
 }
}
}

Source Code: Program to reverse a Stack in C#

The output of the above program is given below:

Current stack...
F
E
D
C
B
A
Reversed stack...
A
B
C
D
E
F

Count the number of elements in Stack

To count the number of elements in Stack, use the Count property:

using System;
using System.Collections;
namespace StackExample {
  class Example {
     static void Main(string[] args) {
        Stack st = new Stack();
        st.Push('w');
        st.Push('x');
        st.Push('y');
        Console.WriteLine("Count of elements in the Current Stack: "+st.Count);
        Console.WriteLine("Stack = ");
        foreach (char ch in st) {
           Console.Write(ch + " ");
        }
        Console.WriteLine();
        st.Push('s');
        st.Push('t');
        st.Push('u');
        st.Push('v');
        Console.WriteLine("Stack after adding two elements... ");
        foreach (char ch in st) {
           Console.Write(ch + " ");
        }
        Console.WriteLine("\nCount of elements in the Current Stack: "+st.Count);
        Console.WriteLine();
        Console.WriteLine("Pop a value!");
        st.Pop();
        Console.WriteLine("Current stack:");
        foreach (char c in st) {
           Console.Write(c + " ");
        }
        Console.WriteLine("\nCount of elements in the Current Stack: "+st.Count);
        Console.WriteLine("\nPop another value!");
        st.Pop();
        Console.WriteLine("Current stack:");
        foreach (char c in st) {
           Console.Write(c + " ");
        }
        Console.WriteLine("\nCount of elements in the Current Stack: "+st.Count);
     }
  }
}

Source Code: Program to count the number of elements in Stack in C#

The output of the above program is given below:

Count of elements in the Current Stack: 3
Stack = 
y x w 
Stack after adding two elements... 
v u t s y x w 
Count of elements in the Current Stack: 7

Pop a value!
Current stack:
u t s y x w 
Count of elements in the Current Stack: 6

Pop another value!
Current stack:
t s y x w 
Count of elements in the Current Stack: 5
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses