C# - SPLessons

How to use C# Stack Class

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

How to use C# Stack Class

How to use C# Stack Class

Hi Friends, let me give you a intro about "Stack". It is a last-in-first-out (LIFO) collection of objects. When you add an item in the list, it is called Push and when you remove it, it is called Pop. We will see How to use C# Stack Class.

Namespace :
[csharp] using System.Collections; [/csharp]

Step1 :
Please open visual studio, right now i have visual studio 2013 i just opened my visual studio and click on new project.  

[image url=""]/wp-content/uploads/2014/09/Stack-csharp-Splessons1.png[/image]

Step2 :
In the new project window please click on templates visual c# and then click on windows  and on the right side panel just click on console application. Please give a name for the console application and click ok button.  

  [image url=""]/wp-content/uploads/2014/09/Stack-csharp-Splessons2.png[/image]

Step3 :
If you see the visual studio will gives you a predefined template program  for us.  

[image url=""]/wp-content/uploads/2014/09/Stack-csharp-Splessons3.png[/image]

Step4 :
Lets, Build simple console application to Push, Pop and  Display items from Stack.

Main Method :
[csharp] private static bool flag = true; private static Stack stackTable = new Stack(); static void Main(string[] args) { while (flag) { Console.WriteLine("\n1. Push \t2. Pop \n3. Display\t4. Exit"); Console.Write("Please enter your Choice: "); string choice = Console.ReadLine(); switch (choice) { case "1": stackTable = Push(stackTable); break; case "2": stackTable = Pop(stackTable); break; case "3": Display(stackTable); break; case "4": flag = false; break; default: Console.WriteLine("Enter correct choice !!"); break; } } } [/csharp]

Output :
[image url=""]/wp-content/uploads/2014/09/Stack-csharp-Splessons4.png[/image]

Push :
[csharp] private static Stack Push(Stack stackTable) { Console.Write("Enter value into Stack Table : "); string stackValue = Console.ReadLine(); stackTable.Push(stackValue); Display(stackTable); return stackTable; } [/csharp]

Output :
[image url=""]/wp-content/uploads/2014/09/Stack-csharp-Splessons5.png[/image]

Pop :
[csharp] private static Stack Pop(Stack stackTable) { string stackValue = stackTable.Pop().ToString(); Console.WriteLine("Removed value form Stack Table : " + stackValue); Display(stackTable); return stackTable; } [/csharp]

Output :
[image url=""]/wp-content/uploads/2014/09/Stack-csharp-Splessons6.png[/image]

Display :
[csharp] private static void Display(Stack stackTable) { var item = stackTable.GetEnumerator(); Console.WriteLine("Object in Stack Table :"); while(item.MoveNext()) { Console.WriteLine(item.Current); } } [/csharp]

Output :
[image url=""]/wp-content/uploads/2014/09/Stack-csharp-Splessons7.png[/image]

Other Methods :
There are many other methods which you can try.

Count :
Gets the number of elements contained in the Stack.

Clone :
Creates a shallow copy of the Stack.

Peek :
Returns the object at the top of the Stack without removing it.

CopyTo :
Returns an Stack to an existing one-dimensional Array

Clear :
Removes all elements from the Stack. etc.

Enjoy to Code..;)