This method(comes under System.Collections namespace) is used to remove and returns the object at the top of the Stack. This method is similar to the Peek method, but Peek does not modify the Stack.
public virtual object Pop ();
Return Value: It returns the Object removed from the top of the Stack.
Exception : This method will give InvalidOperationException if the Stack is empty.
Below programs illustrate the use of the above-discussed method:
Number of elements in the Stack: 5 Top element of Stack is: GeeksforGeeks Number of elements in the Stack: 4
Number of elements in the Stack: 2 Top element of Stack is: 9 Number of elements in the Stack: 1
- Difference between Method Overriding and Method Hiding in C#
- C# | Clone() Method
- C# | Uri.GetHashCode() Method
- C# | Random.Next() Method
- Main Method in C#
- Decimal.Add() Method in C#
- C# | IndexOfAny() Method
- DateTimeOffset.Add() Method in C#
- C# | Math.Min() Method
- C# | Math.Exp() Method
- C# | Math.Abs() Method | Set - 1
- C# | Uri.FromHex() Method
- C# | Method Parameters
- Stack.Contains() Method in C#
- C# | Math.Max() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.