StackmyStack = new Stack (); myStack.Push("Hello"); myStack.Push("World"); string topItem = myStack.Pop(); Console.WriteLine(topItem); // Output: World
StackIn this example, a Stack of integers is created called "myStack". Three integers are added to the stack with the Push method. The while loop continues to iterate while the count of items in the stack is greater than zero. In each iteration, the Pop method is called to remove the top item and assign it to the variable "currentItem". The value of "currentItem" is then written to the console. Both of these examples use the System.Collections namespace and the Stack class. The Pop method specifically allows you to remove the top item from a Stack, making it useful for implementing a LIFO (Last-In-First-Out) data structure.myStack = new Stack (); myStack.Push(1); myStack.Push(2); myStack.Push(3); while (myStack.Count > 0) { int currentItem = myStack.Pop(); Console.WriteLine(currentItem); }