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

Stack<string> st = new Stack<string>();
st.Push("Apple");
st.Push("Cherry");
st.Push("Apple");
st.Push("Durian");

Console.WriteLine("Stack elements: ");
foreach (string 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: 
Durian
Apple
Cherry
Apple

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