Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create a Stack in C#?
A Stack in C# is a generic collection that follows the Last-In-First-Out (LIFO) principle. Elements are added and removed from the top of the stack. The Stack<T> class is part of the System.Collections.Generic namespace and provides type-safe stack operations.
Syntax
Following is the syntax for creating a Stack −
Stack<T> stackName = new Stack<T>();
Common Stack operations −
stack.Push(item); // Add element to top T item = stack.Pop(); // Remove and return top element T item = stack.Peek(); // Return top element without removing bool exists = stack.Contains(item); // Check if element exists
How Stack Works
Creating and Using a Stack
Basic Stack Operations
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Stack<int> stack = new Stack<int>();
stack.Push(100);
stack.Push(150);
stack.Push(175);
stack.Push(200);
stack.Push(225);
stack.Push(250);
stack.Push(300);
stack.Push(400);
stack.Push(450);
stack.Push(500);
Console.WriteLine("Elements in the Stack:");
foreach(var val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements in the Stack = " + stack.Count);
Console.WriteLine("Does Stack has the element 400? = " + stack.Contains(400));
}
}
The output of the above code is −
Elements in the Stack: 500 450 400 300 250 225 200 175 150 100 Count of elements in the Stack = 10 Does Stack has the element 400? = True
Stack with Pop and Peek Operations
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Stack<int> stack = new Stack<int>();
stack.Push(10);
stack.Push(20);
stack.Push(30);
stack.Push(40);
stack.Push(50);
Console.WriteLine("Original Stack Count: " + stack.Count);
Console.WriteLine("Top element (Peek): " + stack.Peek());
Console.WriteLine("Count after Peek: " + stack.Count);
Console.WriteLine("Popped element: " + stack.Pop());
Console.WriteLine("Count after Pop: " + stack.Count);
Console.WriteLine("Remaining elements:");
foreach (int res in stack) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Original Stack Count: 5 Top element (Peek): 50 Count after Peek: 5 Popped element: 50 Count after Pop: 4 Remaining elements: 40 30 20 10
Common Stack Methods
| Method | Description |
|---|---|
Push(T) |
Adds an element to the top of the stack |
Pop() |
Removes and returns the top element |
Peek() |
Returns the top element without removing it |
Contains(T) |
Checks if an element exists in the stack |
Count |
Gets the number of elements in the stack |
Clear() |
Removes all elements from the stack |
Conclusion
The Stack<T> class in C# provides an efficient LIFO collection for managing elements. Use Push() to add elements, Pop() to remove them, and Peek() to examine the top element without removal. Stacks are ideal for scenarios like function call management, undo operations, and expression evaluation.
Advertisements
