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-how-to-create-a-stack/ below:

C# | How to create a Stack

C# | How to create a Stack

Last Updated : 11 Jul, 2025

Stack() constructor

is used to initialize a new instance of the Stack class which will be empty and will have the default initial capacity.

Stack

represents a last-in, first out collection of object. It is used when you need last-in, first-out access to 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. This class comes under

System.Collections

namespace.

Syntax:
public Stack ();
Important Points: Example 1: csharp
// C# Program to illustrate how
// to create a Stack
using System;
using System.Collections;

class Geeks {

    // Main Method
    public static void Main(String[] args)
    {

        // st is the Stack object
        // Stack() is the constructor
        // used to initializes a new
        // instance of the Stack class
        Stack st = new Stack();
 
        // Count property is used to get the
        // number of elements in Stack
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(st.Count);
    }
}
Example 2: csharp
// C# Program to illustrate how
// to create a Stack
using System;
using System.Collections;

class Geeks {

    // Main Method
    public static void Main(String[] args)
    {

        // st is the Stack object
        // Stack() is the constructor
        // used to initializes a new
        // instance of the Stack class
        Stack st = new Stack();
 
        Console.Write("Before Push Method: ");
        
        // Count property is used to get the
        // number of elements in Stack
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(st.Count);

        // Inserting the elements 
        // into the Stack
        st.Push("Chandigarh");
        st.Push("Delhi");
        st.Push("Noida");
        st.Push("Himachal");
        st.Push("Punjab");
        st.Push("Jammu");

        Console.Write("After Push Method: ");
        
        // Count property is used to get the
        // number of elements in st
        Console.WriteLine(st.Count);
    }
}
Output:
Before Push Method: 0
After Push Method: 6
Reference:

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