Removing the object from the top of the Stack in C#

Stack<T>.Pop Method is used to remove and returns the object at the top of the Stack<T>. This method comes under the System.Collections.Generic namespace.

Syntax:

public T Pop ();

Return Value: It returns the Object which is to be removed from the top of the Stack.

Exception : This method will give InvalidOperationException if the Stack<T> is empty.

Below programs illustrate the use of the above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate the
// use of Stack<T>.Pop() Method
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Creating a Stack of Strings
        Stack<string> myStack = new Stack<string>();
  
        // Inserting the elements into the Stack
        myStack.Push("Geeks");
        myStack.Push("Geeks Classes");
        myStack.Push("Noida");
        myStack.Push("Data Structures");
        myStack.Push("GeeksforGeeks");
  
        Console.WriteLine("Number of elements in the Stack: {0}",
                                                 myStack.Count);
  
        // Retrieveing top element of Stack
        Console.Write("Top element of Stack is: ");
        Console.Write(myStack.Pop());
  
        // printing the no of Stack element
        // after Pop operation
        Console.WriteLine("\nNumber of elements in the Stack: {0}",
                                                    myStack.Count);
    }
}

chevron_right


Output:

Number of elements in the Stack: 5
Top element of Stack is: GeeksforGeeks
Number of elements in the Stack: 4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate the
// use of Stack<T>.Pop() Method
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Creating a Stack of integers
        Stack<int> myStack = new Stack<int>();
  
        // Inserting the elements into the Stack
        myStack.Push(7);
        myStack.Push(9);
  
        Console.WriteLine("Number of elements in the Stack: {0}",
                                                  myStack.Count);
  
        // Retrieveing top element of Stack
        Console.Write("Top element of Stack is: ");
        Console.Write(myStack.Pop());
  
        // printing the no of Stack element
        // after Pop operation
        Console.WriteLine("\nNumber of elements in the Stack: {0}",
                                                    myStack.Count);
    }
}

chevron_right


Output:

Number of elements in the Stack: 2
Top element of Stack is: 9
Number of elements in the Stack: 1

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.