VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/removing-the-object-from-the-top-of-the-stack-in-c-sharp/

⇱ Removing the object from the top of the Stack in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 11 Jul, 2025
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:
Output:
Number of elements in the Stack: 5
Top element of Stack is: GeeksforGeeks
Number of elements in the Stack: 4
Example 2:
Output:
Number of elements in the Stack: 2
Top element of Stack is: 9
Number of elements in the Stack: 1
Reference:
Comment

Explore