C# Stack.Push() technique: Here, we will find out about the Push() strategy for Stack class in C#.
C# Stack.Push() technique
Stack.Push() technique is utilized to embed an item at the highest point of the stack.
Syntax:
void Stack.Push(object obj);
Parameters: It acknowledges an article to be embedded at the highest point of the stack.
Return value: void – it returns nothing.
Example:
declare and initialize a stack:
Stack stk = new Stack();
insertting elements:
stk.Push(100);
stk.Push(200);
stk.Push(300);
stk.Push(400);
stk.Push(500);
Output:
500 400 300 200 100
C# example to embed an article to the stack utilizing Stack.Push() technique
using System;
using System.Text;
using System.Collections;
namespace Test
{
class Program
{
//function to print stack elements
static void printStack(Stack s)
{
foreach (Object obj in s)
{
Console.Write(obj + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//declare and initialize a stack
Stack stk = new Stack();
//insertting elements
stk.Push(100);
stk.Push(200);
stk.Push(300);
stk.Push(400);
stk.Push(500);
//printing stack elements
Console.WriteLine("Stack elements are...");
printStack(stk);
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
Stack elements are...
500 400 300 200 100