Stack.Pop() Method in C#

The Stack.Pop() method in C# is used to remove and return the object at the top of the Stack. This method follows the LIFO (Last In, First Out) principle, where the most recently added element is the first one to be removed.

Syntax

Following is the syntax for the Pop() −

public virtual object Pop();

Return Value

The Pop() method returns the object that was removed from the top of the Stack. If the Stack is empty, it throws an InvalidOperationException.

How Stack.Pop() Works

Stack.Pop() Operation Before Pop() Item C Item B Item A Top Pop() Returns "Item C" After Pop() Item B Item A Top Stack size decreases by 1 after Pop()

Using Stack.Pop() Method

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push("Inspiron");
      stack.Push("Alienware");
      stack.Push("Projectors");
      stack.Push("Monitors");
      stack.Push("XPS");
      stack.Push("Laptop");
      stack.Push("Notebook");
      
      Console.WriteLine("Stack elements...");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements = " + stack.Count);
      Console.WriteLine("Element at the top = " + stack.Peek());
      
      stack.Push("Ultrabook");
      stack.Push("Cameras");
      stack.Push("Keyboards");
      
      Console.WriteLine("\nStack elements...updated");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Element at the top = " + stack.Peek());
      Console.WriteLine("\nCount of elements (updated) = " + stack.Count);
      Console.WriteLine("\nRemoving and returning the element at the top = " + stack.Pop());
      Console.WriteLine("Count of elements (updated) = " + stack.Count);
      
      Console.WriteLine("\nStack elements...updated");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
   }
}

The output of the above code is −

Stack elements...
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Count of elements = 7
Element at the top = Notebook

Stack elements...updated
Keyboards
Cameras
Ultrabook
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Element at the top = Keyboards

Count of elements (updated) = 10

Removing and returning the element at the top = Keyboards
Count of elements (updated) = 9

Stack elements...updated
Cameras
Ultrabook
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron

Using Pop() with Stack Cloning

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push("Inspiron");
      stack.Push("Alienware");
      stack.Push("Projectors");
      stack.Push("Monitors");
      stack.Push("XPS");
      stack.Push("Laptop");
      stack.Push("Notebook");
      
      Console.WriteLine("Stack elements...");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements = " + stack.Count);
      Console.WriteLine("Element Speakers in the stack? = " + stack.Contains("Speakers"));
      
      stack.Push("Headphone");
      stack.Push("Keyboard");
      stack.Push("Earphone");
      
      Console.WriteLine("\nStack elements...updated");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements (updated) = " + stack.Count);
      Console.WriteLine("Element Alienware in the stack? = " + stack.Contains("Alienware"));
      
      Stack stack2 = (Stack)stack.Clone();
      Console.WriteLine("\nStack elements...cloned");
      foreach(string val in stack2) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements (stack2) = " + stack2.Count);
      Console.WriteLine("Top of the Stack (stack2) = " + stack2.Peek());
      Console.WriteLine("\nRemoving and returning the element at the top (stack2) = " + stack2.Pop());
      Console.WriteLine("Count of elements (stack2) = " + stack2.Count);
      
      Console.WriteLine("\nStack elements...(stack2)");
      foreach(string val in stack2) {
         Console.WriteLine(val);
      }
   }
}

The output of the above code is −

Stack elements...
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Count of elements = 7
Element Speakers in the stack? = False

Stack elements...updated
Earphone
Keyboard
Headphone
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Count of elements (updated) = 10
Element Alienware in the stack? = True

Stack elements...cloned
Earphone
Keyboard
Headphone
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Count of elements (stack2) = 10
Top of the Stack (stack2) = Earphone

Removing and returning the element at the top (stack2) = Earphone
Count of elements (stack2) = 9

Stack elements...(stack2)
Keyboard
Headphone
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron

Key Points

  • Pop() removes and returns the top element from the Stack.

  • The Stack size decreases by 1 after each Pop() operation.

  • Throws InvalidOperationException if called on an empty Stack.

  • Use Peek() to view the top element without removing it.

Conclusion

The Stack.Pop() method is essential for implementing LIFO operations in C#. It removes and returns the top element from the Stack, making it perfect for scenarios like undo operations, expression evaluation, and recursive algorithms that require last-in-first-out behavior.

Updated on: 2026-03-17T07:04:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements