How to use C# Stack Class
The Stack class represents a last-in-first-out (LIFO) Stack of Objects. Stack follows the push-pop operations. That is we can Push (insert) Items into Stack and Pop (retrieve) it back . Stack is implemented as a circular buffer. It follows the Last In First Out (LIFO) system. That is we can push the items into a stack and get it in reverse order. Stack returns the last item first. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.
Commonly used methods :
- Push : Add (Push) an item in the Stack data structure
- Pop : Pop return the last Item from the Stack
- Contains: Check the object contains in the Stack
Push : Add (Push) an item in the Stack data structureSyntax
- Object : The item to be inserted.
Pop : Pop return the item last Item from the StackSyntax
- Object : Return the last object in the Stack
Contains : Check the object contains in the StackSyntax
- Object : The specified Object to be search
The following CSharp Source code shows some of important functions in Stack Class:Full Source C#
When you execute this C# program add seven items in the stack . Then it check the count is equal to 7 , if it is seven then pop() the item. The message box will display SaturDay.