Home » C# Stack

C# Stack<T>

C# Stack<T> class is used to push and pop elements. It uses the concept of Stack that arranges elements in LIFO (Last In First Out) order. It can have duplicate elements. It is found in System.Collections.Generic namespace.

C# Stack<T> example

Let’s see an example of generic Stack<T> class that stores elements using Push() method, removes elements using Pop() method and iterates elements using for-each loop.

Output:

tutor Peter James Ratan Irfan Peek element: Irfan Pop: Irfan After Pop, Peek element: Ratan 
Next TopicC# Queue

You may also like