Implementation of C# Stack class
Program.cs
using System.Collections;

Stack st = new Stack();
st.Push("Cherry");
st.Push(65655);
st.Push(45588);
st.Push("Apple");

Console.WriteLine("Stack elements: ");
foreach (var s in st)
{
    Console.WriteLine(s);
}
Console.WriteLine("\nPeek element: " + st.Peek());
Console.WriteLine("Pop: element: " + st.Pop());
Console.WriteLine("Peek element: " + st.Peek());
Console.WriteLine("Number of elements: " + st.Count);
Output
kodingwindow@kw:~/csharp$ dotnet run
Stack elements: 
Apple
45588
65655
Cherry

Peek element: Apple
Pop: element: Apple
Peek element: 45588
Number of elements: 3
kodingwindow@kw:~/csharp$ 
Advertisement