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);

cards.Clear();
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.

Conclusion

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.

Related Articles

C# Image Processing

How To Use Fourier Transform On Images – C# Guide

Fourier transform is an equation that turns normal pixel values into complex numbers. But to know what these complex numbers mean, we should give a little more context to them by...

Posted on by Andraz Krzisnik
Digital Image Watermarking

How To Make Image Watermarking Work With C#

This tutorial shows how image watermarking works by implementing it with C# and we describe various purposes that watermarks have as well.

Posted on by Andraz Krzisnik