Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

C# Arrays

C# Character

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

C# ArrayList

C# Stack

C# Queue

C# HashTable

C# SortedList

Page Stats

Visitor: 282

Stack Class

The System.Collections.Stack class is a kind of collection that works on the principle of Last In First Out (LIFO), means that the item that is added last is executed first.

List some important properties of Stack
S.No.PropertyDescription
countReturns the total count of elements in the Stack.
List some important methods of Stack
S.No.MethodDescription
Push()Add an item at the top of the stack. Syntax: void Push(object obj);
Pop()Removes and returns an item from the top of the stack.
Peek()Returns the top item from the stack.
Contains()Checks whether an item exists in the stack or not.
Clear()Removes all items from the stack.
Push() method - Stack

Push() method is use to add an element into the stack.

Example 1: Add value into Stack using push() method.

Stack st = new Stack();
st.Push(10); //You can add any data type
st.Push(20);
st.Push(30);
st.Push(40);

foreach(int i in st)
{
    Console.WriteLine(i); //get all elements in LIFO style.
}
40
30
20
10
Pop() method - Stack

The Pop() method Removes and Returns the value that was added last from the stack. Calling Pop() method on empty stack will throw InvalidOperationException. Syntax: object Pop();

Example 2: Removes item from the Stack using Pop() method.

Stack st = new Stack();
st.Push(10);
st.Push(20);
st.Push(30);
st.Push(40);

int count =  st.Count;
Console.WriteLine("Number of elements in Stack: {0}", count);

for(int i=1; i<=count; i++)
{
    Console.WriteLine(st.Pop());    
}

Console.Write("Number of elements in Stack: {0}", st.Count);
Number of elements in Stack: 4
40
30
20
10
Number of elements in Stack: 0
Peek() method - Stack

The Peek() method Returns the last (top-most) value from the stack. Calling Peek() method on empty stack will throw InvalidOperationException. Syntax: object Peek();

Example 3: Display value from Stack using Peek() method.

Stack st = new Stack();
st.Push(10);
st.Push(20);
st.Push(30);
st.Push(40);

Console.WriteLine(st.Peek());
Console.WriteLine(st.Peek());
Console.WriteLine(st.Peek());
40
40
40
Contains() method - Stack

The Contains() method checks whether the specified item exists in a Stack or not. Returns true if exists, returns false if not exists. Syntax: bool Contains(object obj);

Example 4: Check value from Stack using Contains() method.

Stack st = new Stack();
st.Push(10);
st.Push(20);
st.Push(30);
st.Push(40);

st.Contains(20); // returns true
st.Contains(50); // returns false
Clear() method - Stack

The Clear() method removes all the elements from the stack. Syntax: void Clear();

Example 5: Clear all the elements from the stack.

Stack st = new Stack();
st.Push(10);
st.Push(20);
st.Push(30);
st.Push(40);

st.Clear();
Console.Write("Number of elements in Stack: {0}", st.Count);
Number of elements in Stack: 0
Updated: 06-Sep-21