C# - Stack.Pop() Method

Learn, how to pop an element from stack using collection framework in C#?
Submitted by IncludeHelp, on November 21, 2017 [Last updated : March 27, 2023]

C# Stack.Pop() Method

This is a method of 'Stack' class, it is used to remove/pop the element from stack.

Syntax

object Pop();

Parameter(s)

  • None

Return Value

Return popped or deleted element from stack.

C# program to pop elopements using 'Pop()' method from a stack

using System;
using System.Collections;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      Stack S = new Stack(5);

      S.Push(10);
      S.Push(20);
      S.Push(30);
      S.Push(40);

      Console.WriteLine("Popped Element: " + S.Pop());
      Console.WriteLine("Popped Element: " + S.Pop());
      Console.WriteLine("Popped Element: " + S.Pop());
      Console.WriteLine("Popped Element: " + S.Pop());
    }
  }
}

Output

Popped Element: 40
Popped Element: 30
Popped Element: 20
Popped Element: 10

Note: In above program, to use 'Stack' class, we need to include System.Collection namespace.

C# Data Structure Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.