C# - Stack.Push() Method | Push Elements to Stack

Learn, how to push an element into stack using collection framework in C#?
Submitted by IncludeHelp, on November 21, 2017 [Last updated : March 27, 2023]

C# Stack.Push() Method

This is a method of 'Stack' class, it is used to pushes the element in stack.

Syntax

void Push(object item);

Parameter(s)

  • item : item to be inserted into stack.

C# program to push element in stack using 'Push' method

using System;
using System.Collections;

namespace ConsoleApplication1
{
    
    class Program
    {
        static void Main()
        {
            Stack S = new Stack(5);

            S.Push(10);
            S.Push(20);
            S.Push(30);
            S.Push(40);

            Console.WriteLine("Elements are pushed successfully");

        }
    }
}

Output

Elements are pushed successfully

Note: In above program, to use 'Stack' class, we need to include System.Collection namespace.

C# Data Structure Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.