Stack collection in C#

By Sourav Kayal Blogs | .NET 4.5 Jun 29, 2013
Here i am going to show how to implement stack collection using C#

Stack Class

Stack handles last in first out data processing mechanism. Stack class in one of Collection class of .NET Class library. You have to include using System.Collections.Generic; namespace in order to use collection class in your program.


Create object of collection class like this

            Stack<Int32> st = new Stack<int>();

And you can call push() or pop() method of collection class in order to insert data and delete data symulteniously.


In this below example I have created one object of collection class which is (st). And I have inserted two element in my Stack class. Then I have called st.Pop(); to delete top item of my Stack Collection. Then I am displaying the top most item by calling Peek() function.



And as I have deleted my top most item from stack now my top item is 100.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace BlogProject


    class Program


        static void Main(string[] args)


            Stack<Int32> st = new Stack<int>();









 Result:- 100



Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs