Related Articles

Back to Latest Articles

How To Use Stacks In C# – Data Structures Made Easy

Stacks are limited access data structures, which means we can't access every element. It only allows us to access the last object we added.

Andraz Krzisnik
How To Use Stacks In C# – Data...

Stacks are special data structures in C# that fall under limited access data structures category. Therefore, we can only access certain objects, but not all of them.

I didn’t mention this with arrays or lists before, but those fall under the random access data structures. In other words, we can access any element and change it or remove it. As I mentioned before, this is not the case with stacks.

First of all, imagine that you have a deck of cards and you can only draw the top card. You can also add cards to this deck. However, you can only place each of them at the top as well. This is basically how stacks work, it’s a last in first out principle.

They have 3 vital methods for their use, which are Push, Pop and Peek. Firstly, Push method will add an object to the top of the stack. Secondly, Pop will remove the last object that we added and return its info. And lastly, Peek will return the info of the top object without changing the stack.

However, there are other methods we could use, like Clear, which will remove all objects stored inside and Contains, which will check if a certain object exists.

Stacks in C#

Let’s take a look at some C# code to see how ti works in practice.

Stack<string> cards = new Stack<string>();

cards.Push("King of Hearts");
cards.Push("Ace of Spades");
cards.Push("Queen of Clubs");
cards.Push("Jack of Diamonds");
Console.WriteLine("Total cards left: " + cards.Count);

string draw = cards.Pop();
Console.WriteLine("You've drawn: " + draw);
Console.WriteLine("Total cards left: " + cards.Count);

string peek = cards.Peek();
Console.WriteLine("Top card of the deck is: " + peek);
Console.WriteLine("Total cards left: " + cards.Count);

string answer = cards.Contains("Ace of Spades") ? "Yes" : "No";
Console.WriteLine("Ace of Spades in deck? " + answer);

Console.WriteLine("Total cards left: " + cards.Count);

For this example I chose to set up a deck of 4 cards and I added each with Push method. Furthermore, by using the Count property, we can confirm that it indeed holds 4 cards.

Next, we can see the Pop method at work, which removed the last card I added and returned its name.

Similarly, by using Peek method, which only returned the name of the last card. Since we already used Pop, it’s going to return the second last card we added in the beginning.

And lastly, we can see Contains and Clear methods at work, of which results are displayed as well.


I hope this short guide helped you gain a better understanding about how stacks in C# work.

You can also download the project I set up for this demonstration and try it out yourself.

Show Comments (0)


Related Articles

C# Tutorial

Intensity Level Slicing With C# – Explore Image Processing

In this tutorial we will be talking about intensity level slicing in image processing. What is Intensity Level Slicing It’s a process that highlights pixels in an arbitrary...

Posted on by Andraz Krzisnik
Intensity Slicing and Color Coding

How To Use False Color Coding And Intensity Slicing – C#

False color or pseudocolor image processing coupled with intensity slicing is useful for emphasizing shapes that might be hidden to our eyes.

Posted on by Andraz Krzisnik