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.
Syntax:
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:
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: