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# | Insert() Method
- C# | Replace() Method
- C# | Clone() Method
- Decimal.Add() Method in C#
- C# | Math.Log() Method
- C# | ToCharArray() Method
- C# | Random.Next() Method
- C# | IsNullOrWhiteSpace() Method
- Stack.Contains() Method in C#
- C# | PadRight() Method
- C# | Uri.ToString() Method
- C# | Uri.IsWellFormedOriginalString() Method
- C# | EndsWith() Method
- C# | StartsWith() 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 email@example.com. 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.