
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Push vs pop in stack class in C#
Stack class represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items.
The following is the property of Stack class −
Count − Gets the number of elements in the stack.
Push Operation
Add elements in the stack using the Push operation −
Stack st = new Stack(); st.Push('A'); st.Push('B'); st.Push('C'); st.Push('D');
Pop Operation
The Pop operation removes elements from the stack starting from the element on the top.
Here is an example showing how to work with Stack class and its Push() and Pop() method −
Using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push('A'); st.Push('B'); st.Push('C'); st.Push('D'); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); st.Push('P'); st.Push('Q'); Console.WriteLine("The next poppable value in stack: {0}", st.Peek()); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); Console.WriteLine("Removing values...."); st.Pop(); st.Pop(); st.Pop(); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } } } }
- Related Articles
- stack push() and pop() in C++ STL
- C# Program to Implement Stack with Push and Pop operations
- queue::push() and queue::pop() in C++ STL
- Validating push pop sequence in JavaScript
- Stack Class in C#
- Array push(), pop() and clear() functions in Ruby
- Push() vs unshift() in JavaScript?
- Shift() vs pop() methods in JavaScript?
- Debug Class vs Debugger Class in C#
- How to push and pop elements in a queue in Ruby?
- How to use Stack class in C#?
- What is the Stack class in C#?
- Structure vs class in C++
- How to use Push-Location and Pop-Location command in PowerShell?
- Abstract vs Sealed Classes vs Class Members in C#

Advertisements