Removing the object from the top of the Stack in C# Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: csharp // 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); } } Output: Number of elements in the Stack: 5 Top element of Stack is: GeeksforGeeks Number of elements in the Stack: 4 Example 2: csharp // 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); } } Output: Number of elements in the Stack: 2 Top element of Stack is: 9 Number of elements in the Stack: 1 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1.pop?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | How to remove the element from the specified index of the List K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Generic-Stack CSharp-Generic-Namespace Similar Reads C# | Remove all objects from the Stack Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.Clear Method is used to Removes all objects 3 min read C# | Get object at the top of the Stack - Peek operation Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.Peek Method is used to returns the object a 3 min read C# | Insert an object at the top of the Stack - Push Operation Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.Push(T) Method is used to inserts an object 2 min read C# | How to remove the element from the specified index of the List List<T>.RemoveAt (Int32) Method is used to remove the element at the specified index of the List<T>. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for reference types and it also allow 3 min read C# | Removing the node at the start of the LinkedList<T> LinkedList<T>.RemoveFirst method is used to remove the node at the start of the LinkedList<T>. Syntax: public void RemoveFirst (); Exception: The method throws InvalidOperationException if the LinkedList<T> is empty. Below given are some examples to understand the implementation in 2 min read C# | Removing the specified element from the List List.Remove(T) Method is used to remove the first occurrence of a specific object from the List. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for reference types and it also allows duplicate elem 2 min read Like