C# Stack - Pop() Method



The C# stack Pop() method is used to remove and return the object at the top of the stack. The stack follows the last in first out (LIFO) principle, which means the most recently added object is removed first.

This method is similar to the Peek method, but the Peek method does not modify the stack.

Syntax

Following is the syntax of the C# stack Pop() method −

public virtual object? Pop ();

Parameters

This method does not accepts any parameters.

Return value

This method returns an object removed from the top of the stack.

Example 1: Remove and Return Top element from Stack

Following is the basic example of the Pop() method to remove the element at the top of the stack and return it −

using System;
using System.Collections.Generic;

class Example1 {
   static void Main() {
      Stack<int> numbers = new Stack<int>();
      // Pushing elements onto the stack
      numbers.Push(10);
      numbers.Push(20);
      numbers.Push(30);
      
      Console.WriteLine("Popped Element: " + numbers.Pop());
      Console.WriteLine("Popped Element: " + numbers.Pop());
      
      // Remaining stack elements
      Console.WriteLine("Elements left in stack: " + numbers.Count);
   }
}

Output

Following is the output −

Popped Element: 30
Popped Element: 20
Elements left in stack: 1

Example 2: Handling Empty Scenario

Let's see another example of the Pop() method. Here, we first check the count if count is more than one we remove the element from the stack −

using System;
using System.Collections.Generic;
class Example1 {
   static void Main() {
      Stack<int> numbers = new Stack<int>();
   
      // Pushing elements onto the stack
      numbers.Push(10);
      numbers.Push(20);
      numbers.Push(30);
   
      if (numbers.Count > 0) {
        int topElement = numbers.Pop();
        Console.WriteLine("Popped: " + topElement);
      }
      
      else {
        Console.WriteLine("Stack is empty, cannot pop.");
      };
   
      // Remaining stack elements
      Console.WriteLine("Elements left in stack: " + numbers.Count);
   }
}

Output

Following is the output −

Stack after Pop operations:
C#
World
Hello

Example 3: Pop The String from the Top Of the Stack

The below example uses the Pop() method to pop the string from the top of the stack −

using System;
using System.Collections.Generic;
class Example1 {
   static void Main() {
      Stack<string> stack = new Stack<string>();

      // Pushing elements onto the stack
      stack.Push("Pvt LTD");
      stack.Push("India");
      stack.Push("Tutorialspoint");

      if (stack.Count > 0) {
         string topElement = stack.Pop();
         Console.WriteLine("Popped: " + topElement);
      }
      else {
         Console.WriteLine("Stack is empty, cannot pop.");
      }
      // Remaining stack elements
      Console.WriteLine("Elements left in stack: " + stack.Count);
   }
}

Output

Following is the output −

Popped: Tutorialspoint
Elements left in stack: 2

Example 4: Undo Feature Using Pop Method

Here, in this example, we use the Pop method to create undo features in the stack, we can remove the last updated object −

using System;
using System.Collections.Generic;
class Example4 {
   static void Main() {
      Stack<string> undoStack = new Stack<string>();

      // Simulating text changes
      undoStack.Push("Hello");
      undoStack.Push("Hello, World!");
      undoStack.Push("Hello, World! How are you?");
      undoStack.Push("Hello, World! How are you? I am fine.");

      Console.WriteLine("Current Text: " + undoStack.Peek()); 
      
      // Undo the last action (Pop the last change)
      Console.WriteLine("\nUndoing last change...");
      undoStack.Pop();

      // Display the updated text
      Console.WriteLine("After Undo: " + undoStack.Peek());

      // Undo again
      Console.WriteLine("\nUndoing one more change...");
      undoStack.Pop();
      Console.WriteLine("After Undo: " + undoStack.Peek());

      // Remaining in stack
      Console.WriteLine("\nRemaining elements in Undo Stack: " + undoStack.Count);
   }
}

Output

Following is the output −

Current Text: Hello, World! How are you? I am fine.

Undoing last change...
After Undo: Hello, World! How are you?

Undoing one more change...
After Undo: Hello, World!

Remaining elements in Undo Stack: 2
csharp_stack.htm
Advertisements