In Stack, Each element is added to the top. Each element we remove is removed from the top. This is a LIFO collection—the stack is last-in-first-out.
Common Methods :
Push. Usually, the first action you need to do on Stack is Push elements into it. The word Push is a computer science term that means "add to the top."
Example :
using System;
using System.Collections;
using
System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
using System;
using System.Collections;
public class Program
{
public static void Main()
{
Stack myFirstStack = new Stack();
myFirstStack.Push(1);
myFirstStack.Push(2);
myFirstStack.Push(3);
myFirstStack.Push(4);
myFirstStack.Push(5);
myFirstStack.Push("Hello Nitin
!!");
myFirstStack.Push(null);
foreach (var itm in myFirstStack)
Console.WriteLine(itm);
Console.Read();
}
}
}
|
output:
==========
Example 2: with multiple data types data ( string, int and null values )
using System;
using System.Collections;
using
System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
using System;
using System.Collections;
public class Program
{
public static void Main()
{
Stack myFirstStack = new Stack();
myFirstStack.Push(1);
myFirstStack.Push(2);
myFirstStack.Push(3);
myFirstStack.Push(4);
myFirstStack.Push(5);
myFirstStack.Push("Hello Nitin
!!");
myFirstStack.Push(null);
foreach (var itm in myFirstStack)
Console.WriteLine(itm);
Console.Read();
}
}
}
|