C# Programming Language: Stack
Hi everyone, welcome back. In this tutorial, we will be going over the Stack and how to use the built in functions regarding them. A stack is a linear type of data structure that is capable of storing objects. The stack is a last-in-first-out or LIFO data structure which means that the last item placed into the stack is the first item that comes off of the stack. With this introduction out of the way, let’s get into it.

Creating The Stack
Let’s start by creating a Stack. Before we can create the Stack, we need to make sure we have our Stack import, which is included within the System.Collections.Generic package:
using System.Collections.Generic;
Now, we can successfully create our Stack:
Stack<int> myStack = new Stack<int>();
So now, we have created our Stack variable of the ‘int’ data type and named it ‘myStack.’ When we create our Stack, we need to specify the data type. As we can see in the example above, we have used the ‘int’ data type. We can use other data types as well, such as the string and boolean data types.
Adding Elements to The Stack
Now, let’s see how we can add an element to the Stack. We can do this by using the built in Push() function that is provided. Let’s see an example:
Stack<int> myStack = new Stack<int>();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
Console.WriteLine(String.Join(" ", myStack));Output:
3 2 1
In the example above, we can see that we added a few elements into our Stack by using the Push() function. We can verify that they were successfully added by printing our Stack and checking the output.
Accessing The Top Element of The Stack
Now, let’s see how we can view the top element of the Stack. We can do this by using the built in Peek() function that is provided. Let’s see an example:
Stack<int> myStack = new Stack<int>();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
Console.WriteLine(myStack.Peek());Output:
3
In the example above, we can see that we used the Peek() function to retrieve the top element of our Stack. We can verify that we retrieved the value by printing the result and checking the output.
Removing The Top Element of The Stack
Now, let’s see how we can remove the top element of the Stack. We can do this by using the built in Pop() function that is provided. The Pop() function will also return the top element of the Stack. Let’s see an example:
Stack<int> myStack = new Stack<int>();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
Console.WriteLine(myStack.Pop());
Console.WriteLine(String.Join(" ", myStack));Output:
3
2 1
In the example above, we can see that we have added a few elements into our Stack. We used the Pop() function to return and remove the top element of our Stack. We can verify that the Pop() function returns the top element by printing the result and checking the output. We can also verify that the Pop() function successfully removed the top element by printing the Stack and noting that the top element is no longer there.
Clearing Out The Stack
Now, let’s see how we can completely clear out the Stack. We can do this by using the built in Clear() function that is provided. Let’s see an example:
Stack<int> myStack = new Stack<int>();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Clear();
Console.WriteLine(String.Join(" ", myStack));Output:
In the example above, we have added a few elements into our Stack. Then we used the Clear() function on our Stack. We can verify that all elements were removed by printing our Stack and checking the output. Our output should be empty because we removed everything.
Checking Elements in a Stack
Now, let’s see how we can check if an element exists in a Stack. We can do this by using the built in Contains() function that is provided. We will need to specify the value of the element to check for. Let’s see an example:
Stack<int> myStack = new Stack<int>();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
Console.WriteLine(myStack.Contains(2));
Console.WriteLine(myStack.Contains(4));Output:
True
False
In the example above, we can see that we added a few numbers into our Stack. Then we printed the results of the Contains() function. We checked to see if our Stack contained the value ‘2’ and the value ‘4.’ As we can see in the output, our Stack contains a value of ‘2,’ but not ‘4.’
Checking The Size of a Stack
Now, let’s see how we can check the size of the Stack, or how many elements are in the Stack. We can do this by using the built in Count property that is provided. Let’s see an example:
Stack<int> myStack = new Stack<int>();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
Console.WriteLine(myStack.Count);Output:
3
In the example above, we can see that we added a few numbers into our Stack. We then printed the result of the Count property on our Stack. As we can see in the output, our Stack contains 3 elements.
Conclusion
That is it for the C# Stack examples. We covered how to create Stacks along with some of the functions regarding them. I hope this helps. If there are any questions or comments, please let me know. Thanks for reading.