Open In App

C# | How to create a Stack

Last Updated : 18 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • The number of elements that a Stack can hold is known as the Capacity of the Stack. If the elements will be added to the Stack then capacity will be automatically increased by reallocating the internal array.
  • Specifying the initial capacity will eliminate the requirement to perform a number of resizing operations while adding elements to the Stack if the size of the collection can be estimated.
  • This constructor is an O(1) operation.

Example 1:




// 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);
    }
}


Output:

0

Example 2:




// 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:



Previous Article
Next Article

Similar Reads

C# | Create a Stack from a collection
Stack represents a last-in, first out collection of object. It is used when you need a 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. Creation of stack means the addition of item into the stack. Stack<T>.Push(Object) Method is used to I
3 min read
Stack.Peek Method in C#
This method(comes under System.Collections namespace) is used to return the object at the top of the Stack without removing it. This method is similar to the Pop method, but Peek does not modify the Stack. Syntax: public virtual object Peek (); Return Value: It returns the Object at the top of the Stack. Exception: Calling Peek() method on empty st
2 min read
C# | Check if a Stack contains an element
Stack represents a last-in, first out collection of object. Stack<T>.Contains(Object) Method is used to check whether an element is in the Stack<T> or not. Syntax: public virtual bool Contains(object obj); Return Value: The function returns True if the element exists in the Stack<T> and returns False if the element doesn't exist i
2 min read
C# | Remove all objects from the Stack
Stack represents a last-in, first out collection of object. It is used when you need a 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. Stack<T>.Clear Method is used to Removes all objects from the Stack<T>. This method is an O(n) o
3 min read
C# | Get the number of elements contained in the Stack
Stack represents a last-in, first out collection of object. Stack<T>.Count Property is used to gets the number of elements contained in the Stack. Retrieving the value of this property is an O(1) operation. Syntax: myStack.Count Here myStack is the name of the Stack<T> Return Value: The property returns the number of elements contained
2 min read
C# | Get object at the top of the Stack - Peek operation
Stack represents a last-in, first out collection of object. It is used when you need a 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. Stack<T>.Peek Method is used to returns the object at the top of the Stack<T> without removing i
3 min read
C# | Insert an object at the top of the Stack - Push Operation
Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of 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. Stack<T>.Push(T) Method is used to inserts an object at the top of the Stack<T>. Properties: The
2 min read
C# | Convert Stack to array
Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of 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. Stack<T>.ToArray Method is used to copy a Stack<T> to a new array. Properties: The capacity of a
2 min read
C# | Copy the Stack to an Array
Stack<T>.CopyTo(T[], Int32) Method is used to copy the Stack<T> to an existing 1-D Array which starts from the specified array index. Properties: The capacity of a Stack<T>is the number of elements the Stack<T> can hold. As elements are added to a Stack<T> , the capacity is automatically increased as required through r
2 min read
C# | Stack<T>.TrimExcess Method with Examples
Stack represents a last-in, first out collection of object. It is used when you need a 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. Stack<T>.TrimExcess Method is used to set the capacity to the actual number of elements in the Queue<T
2 min read