Summary: in this tutorial, you will learn how to use the C# Stack<T>
to store elements in a collection based on the last-in-first-out (LIFO) principle.
Introduction to the C# Stack<T> type
Suppose you have a stack of books on your desk. When you get a new book, you place it on top of the stack. The top book is always the one you can easily pick for reading.
When you want to read a book, you take the one from the top of the stack, revealing the next book below.
In this analogy, the stack of books represents a data structure where you add and remove books in a first-in-last-out order (LIFO). The first book that you place on the stack is the last one that you remove from the stack.
In C#, Stack<T>
class works the same way as a stack of books. The Stack<T>
has two main methods:
Push()
– places an item on the top of the stack.Pop()
– removes the item from the top of the stack.
The Stack<T>
also has the Count
property that returns the number of elements of the stack.
Because the Stack<T>
is a generic class, you store values of any type T
in the stack.
C# Stack<T> example
The following example illustrates how to use the Stack<T>
class to manage a stack of books:
using static System.Console;
var bookStack = new Stack<string>();
var titles = new List<string>{
"C# Tutorial",
"Mastering LINQ",
"A Complete Guide to EF Core"
};
// push
foreach (var title in titles)
{
WriteLine($"Push the book '{title}' onto the stack.");
bookStack.Push(title);
}
WriteLine($"The stack has {bookStack.Count} book(s).");
// pop
while (bookStack.Count > 0)
{
var title = bookStack.Pop();
WriteLine($"Pop the book '{title}' out of the stack.");
}
WriteLine($"The stack has {bookStack.Count} book(s).");
Code language: C# (cs)
Output:
Push the book 'C# Tutorial' onto the stack.
Push the book 'Mastering LINQ' onto the stack.
Push the book 'A Complete Guide to EF Core' onto the stack.
The stack has 3 book(s).
Pop the book 'A Complete Guide to EF Core' out of the stack.
Pop the book 'Mastering LINQ' out of the stack.
Pop the book 'C# Tutorial' out of the stack.
The stack has 0 book(s).
Code language: plaintext (plaintext)
How it works.
First, create a new stack that stores the strings representing the book titles:
var bookStack = new Stack<string>();
Code language: C# (cs)
Second, initialize a list of book titles:
var titles = new List<string>{
"C# Tutorial",
"Mastering LINQ",
"A Complete Guide to EF Core"
};
Code language: C# (cs)
Third, iterate over the book titles and push each of them onto the stack using a foreach loop and Push()
method:
foreach (var title in titles)
{
WriteLine($"Push the book '{title}' onto the stack.");
bookStack.Push(title);
}
Code language: C# (cs)
Fourth, check the stack’s size using the Count
property:
WriteLine($"The stack has {bookStack.Count} book(s).");
Code language: C# (cs)
Fifth, remove each book from the stack using the Pop()
method until the stack is empty:
while (bookStack.Count > 0)
{
var title = bookStack.Pop();
WriteLine($"Pop the book '{title}' out of the stack.");
}
Code language: C# (cs)
Finally, shows the stack’s size:
WriteLine($"The stack has {bookStack.Count} book(s).");
Code language: C# (cs)
Summary
- Use C#
Stack<T>
to manage a collection of elements in the last-in, first-out (LIFO) order. - Use
Push()
method to push an element onto the stack. - Use
Pop()
method to pop an element out of the stack. - Use
Count
property to get the number of elements on the stack.