- 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# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- 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# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
C# - Stack Class
It 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.
Methods and Properties of the Stack Class
The following table lists some commonly used properties of the Stack class −
| Sr.No. | Property & Description |
|---|---|
| 1 |
Count
Gets the number of elements contained in the Stack. |
The following table lists some of the commonly used methods of the Stack class −
| Sr.No. | Method & Description |
|---|---|
| 1 |
public virtual void Clear();
Removes all elements from the Stack. |
| 2 |
public virtual bool Contains(object obj);
Determines whether an element is in the Stack. |
| 3 |
public virtual void CopyTo (Array array, int index);
Copies the Stack to an existing one-dimensional Array. |
| 4 |
public virtual object Clone();
Creates a shallow copy of the Stack. |
| 5 |
public virtual object Peek();
Returns the object at the top of the Stack without removing it. |
| 6 |
public virtual object Pop();
Removes and returns the object at the top of the Stack. |
| 7 |
public virtual void Push(object obj);
Inserts an object at the top of the Stack. |
| 8 |
public virtual object[] ToArray();
Copies the Stack to a new array. |
Example
The following example demonstrates use of Stack −
using System;
using System.Collections;
namespace CollectionsApplication {
class Program {
static void Main(string[] args) {
Stack st = new Stack();
st.Push('A');
st.Push('M');
st.Push('G');
st.Push('W');
Console.WriteLine("Current stack: ");
foreach (char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
st.Push('V');
st.Push('H');
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 + " ");
}
}
}
}
When the above code is compiled and executed, it produces the following result −
Current stack: W G M A The next poppable value in stack: H Current stack: H V W G M A Removing values Current stack: G M A