VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/stack-class-in-c-sharp/

⇱ Stack Class in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Stack Class in C#

Last Updated : 20 Apr, 2026

A Stack<T> in C# is a collection that stores elements in LIFO (Last In, First Out) order. This means the last element inserted is the first one removed. It is defined in the System.Collections.Generic namespace and provides fast insertion and removal from the top of the stack.

  • LIFO order: Last element pushed is the first to be popped.
  • Generic type-safe collection (Stack<int>, Stack<string>, etc.).
  • Duplicates allowed (unlike sets).
  • Dynamic resizing: Capacity grows automatically as needed.
  • Efficient operations: Push, Pop, and Peek work in O(1) time.

Example: This example demonstrates how to use Stack to push and pop elements in LIFO order.


Output
4
3
2
1

Declaration of Stack

1. Generic Stack: Holds elements of a specific type.

Stack<T> stackName = new Stack<T>();

2. Non-Generic Stack: Can hold elements of any type but requires casting when retrieving elements.

Stack stack = new Stack(); // Non-generic stack

Example: Example demonstrating both generic and non-generic Stack usage


Output
Non-Generic Stack:
Geeks123

2.5
1
GFG

Generic Stack:
Stack Example
GeeksForGeeks
GFG

Constructors

The Stack<T> class provides three constructor which are listed below in the table:

Constructor

Description

Stack()

Initializes an empty stack with the default initial capacity.

Stack(ICollection)

Initializes a new stack containing elements copied from a specified collection.

Stack(Int32)

Initializes an empty stack with the specified initial capacity (or the default if the given capacity is smaller).

Example: This example demonstrates the basic operations of Stack such as adding an element, checking the Stack size and viewing the top element without modify the stack.


Output
Total elements in Stack: 6
Top element is: 60
Top element again is: 60
Updated count of elements: 6

Properties

The Stack<T> class provides several properties to access its state.

Property

Description

Count

Returns the number of elements currently in the stack.

IsSynchronized

Indicates whether the stack is thread-safe.

SyncRoot

Provides an object to synchronize access to the stack.

Example: This example demonstrates the total number of elements in the Stack.


Output
Total number of elements in the Stack are: 6

Methods

MethodDescription
Clear()Removes all objects from the Stack.
Clone()Creates a shallow copy of the Stack.
Contains(Object)Determines whether an element is in the Stack.
CopyTo(Array, Int32)Copies the Stack to an existing one-dimensional Array, starting at the specified array index.
Equals(Object)Determines whether the specified object is equal to the current object.
GetEnumerator()Returns an IEnumerator for the Stack.
GetHashCode()Serves as the default hash function.
GetType()Gets the Type of the current instance.
MemberwiseClone()Creates a shallow copy of the current Object.
Peek()Returns the object at the top of the Stack without removing it.
Pop()Removes and returns the object at the top of the Stack.
Push(Object)Inserts an object at the top of the Stack.
Synchronized(Stack)Returns a synchronized (thread safe) wrapper for the Stack.
ToArray()Copies the Stack to a new array.
ToString()Returns a string that represents the current object.

Example: This example demonstrates how to use Clear() to remove all the elements from the stack.


Output
Total elements in Stack before clear: 6
Total elements in Stack after clear: 0

Example: This example demonstrates how to check if a specific element is present in a stack using the Contains().


Output
The element Geek2 is present? :True
The element Geek10 is present? :False
Comment

Explore