A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-stack-with-examples/ below:

C# Stack with Examples - GeeksforGeeks

C# Stack with Examples

Last Updated : 11 Jul, 2025

In C# a Stack is a Collection that follows the Last-In-First-Out (LIFO) principle. It is used when we need last-in, first-out access to items. In C# there are both generic and non-generic types of Stacks. The generic stack is in the System.Collections.Generic namespace, while the non-generic stack is in System.Collections. The stack can dynamically store elements of the same or different types.

Example: This example demonstrates the basic working of Stack.

C#
// C# Program Implementing Stack Class 
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main(string[] args)
    {
        // Create a new stack
        Stack<int> s = new Stack<int>();

        // Push elements onto the stack
        s.Push(1);
        s.Push(2);
        s.Push(3);
        s.Push(4);

        // Pop elements from the stack
        while (s.Count > 0)
        {
            Console.WriteLine(s.Pop());
        }
    }
}

Below is the diagramatic Representation of Working of Stack:

Hierarchy of Stack Class

The below diagram shows the Hierarchy of Stack class.

Creating a Stack

Stack class has three constructors which are used to create a stack which is as follows:

Let’s see how to create a stack using Stack() constructor:

Step 1: Include System.Collections namespace in your program with the help of using keyword

using System.Collections;

Step 2: Create a Stack using Stack class

Stack s = new Stack();

Performing Various Operations on Stack

1. Adding Elements: We use push() method to insert elements in a stack.

Example: This example demonstrates how to create non-generic stack, add various elements to it and access them in Last-In-FIrst-Out (LIFO) Order.

C#
// C# program to demonstrates how to
// create and add elements into a stack
using System;
using System.Collections;

class Geeks {

    static public void Main()
    {

        // Create a stack
        // Using Stack class
        Stack s = new Stack();

        // Adding elements in the Stack
        // Using Push method
        s.Push("Geek");
        s.Push("geeksforgeeks");
        s.Push(null);
        s.Push(1);
        s.Push(10.0);

        // Accessing the elements
        // of s Stack
        // Using foreach loop
        foreach(var elem in s) { 
          Console.WriteLine(elem); 
        }
    }
}

Output
10
1

geeksforgeeks
Geek

2. Removing Elements:The Stack class provides two different methods to remove elements and the methods are listed below:

Example: This example displays the contents of the stack before and after removal.

C#
// C# Program to remove elements
// from a stack
using System;
using System.Collections.Generic;

class Geeks {
    public static void Main(string[] args)
    {
        // Initialize a stack
        Stack<string> s = new Stack<string>();

        // Inserting elements into the s using Push()
        s.Push("Geeks");
        s.Push("For");
        s.Push("Geeks");
        s.Push("For");

        // Initial stack
        Console.WriteLine("Initial stack: ");
        foreach(var item in s) { 
          Console.WriteLine(item); 
        }

        // Removing the top element 
        // from the stack
        s.Pop();

        // Final s after removal
        Console.WriteLine("\nUpdated stack after Pop:");
        foreach(var item in s) { 
          Console.WriteLine(item); 
        }
    }
}

Output
Initial stack: 
For
Geeks
For
Geeks

Updated stack after Pop:
Geeks
For
Geeks

3. Get the Topmost Element of the Stack: The Stack class provides two different methods to find the topmost element of the stack and the methods are listed below:

Example: This example demonstrates how to peek at the topmost element of a Stack.

C#
// C# Program to get the
// topmost element of the Stack
using System;
using System.Collections.Generic;

class Geeks {
    public static void Main(string[] args)
    {
        // Create a new stack of integers
        Stack<int> s1 = new Stack<int>();

        // Push elements onto the stack
        s1.Push(10);
        s1.Push(20);
        s1.Push(30);

        // Checking if the stack is not empty before
        // accessing the topmost element
        if (s1.Count > 0) {
          
            // Peek() returns the topmost element without
            // removing it
            int t = s1.Peek();
            Console.WriteLine(
                "The topmost element in the stack is: "
                + t);
        }
        else {
            Console.WriteLine("The stack is empty.");
        }
    }
}

Output
The topmost element in the stack is: 30

4. Checking the Availability of Elements in the Stack: Stack class provide Contains() method to check if the element is present in the Stack or not.

Example:

C#
// C# Program to to check the 
// availability of elements in the stack
using System;
using System.Collections.Generic;

class Geeks {
    public static void Main(string[] args)
    {
        // Create a new stack of integers
        Stack<int> s = new Stack<int>();

        // Push elements onto the stack
        s.Push(10);
        s.Push(20);
        s.Push(30);

        // Check if the element 20 is present in the stack
        Console.WriteLine(
            "The element 20 is present in the stack: "
            + s.Contains(20));

        Console.WriteLine(
            "The element 100 is present in the stack: "
            + s.Contains(100));
    }
}

Output
The element 20 is present in the stack: True
The element 100 is present in the stack: False
Generic Stack vs Non-Generic Stack

Generic Stack

Non-Generic Stack

Generic stack is defined under System.Collections.Generic namespace.

Non-Generic stack is defined under System.Collections namespace.

Generic stack can only store same type of elements.

Non-Generic stack can store same type or different types of elements.

There is a need to define the type of the elements in the stack

There is no need to define the type of the elements in the stack.

It is type-safe.

It is not type-safe.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4