Introduction and Background
In this article, we are going to tackle the basics of Stack class which behaves in a simple last-in-first-out (LIFO) manner. The idea behind this article is combining the two subjects which are non-generic and generic Stack class with code samples. So, readers can see the differences in the usage of the two.
What is a Stack Collection?
The Stack is a specialized type of collection that stores elements in a last-in-first-out manner. In other words, within the stack collection, you can only add and/or remove an item is at the top of the stack. It is also good to mention that it is available within the generic and non-generic collection namespace.
Let us see the important methods and properties that you need to be familiar with in order to use this specialized class collection.
Method
|
Definition/Usage
|
Push
|
Add an item to the stack collection
|
Peek
|
Checks the top item of the stack collection
|
Pop
|
Removes the current top item of the stack collection
|
Contains
|
Checks whether the stack collection contains a certain item
|
Clear
|
Removes all the items within the stack collection
|
What is the Difference between non-generic Stack to a generic Stack collection?
Of course, for experienced developers the answer is obvious. You need to know the difference between the generic collections and non-generic collections to know the difference. Let’s get back to that later.
However; for beginners let me share my experience. While I was doing the sample codes, I could answer with certainty the main difference between the two non-generic and generic Stack class. See my answers in the bullet list below.
- When using a non-generic Stack class, you can add any object within the collection. Meaning you can add anything, it could be a string, number, custom type.
- When using a generic Stack class, you can only add the type that you have declared when you have initialized it.
Therefore; it all boils down to the concept of “Generics”. Moreover; to answer the difference between the generic collections and non-generic collections. Just remember non-generic collections store elements internally as objects while generic collections store elements internally as actual types.
Wo-hoo! Hopefully, that makes sense. Now, let’s get started by seeing sample codes.
Code Samples
Before we start, I would like you to see the Book class which is used with the entire sample codes, specifically with the Stack generic collection class.
- using System;
- using System.Diagnostics.CodeAnalysis;
-
- namespace Stacks_Non_Generic_And_Generic_Csharp_101.Model
- {
- public enum BookCategory
- {
- IT = 0,
- CRIME = 1,
- SPORTS = 2
- }
-
- public class Book : IEquatable<Book>
- {
- public int Id { get; set; }
-
- public string Title { get; set; }
-
- public BookCategory BookCategory { get; set; }
-
- public bool Equals([AllowNull] Book other)
- {
- return this.Title == other.Title;
- }
-
- public override string ToString()
- {
- return this.Title;
- }
- }
- }
Different ways to create a new instance of the Stack object.
- [Fact]
- public void Test_Different_Ways_To_Create_New_Instance_Stack_NonGeneric()
- {
-
- Stack stackofBooks = new Stack();
-
-
- Assert.NotNull(stackofBooks);
-
-
- Assert.True(stackofBooks.Count == 0);
-
-
- string[] books = new string[] { "C# in Depth", "Pro C# 7", "C# 5.0 In A Nutshell" };
-
-
- Stack stackOfBooks_Csharp = new Stack(books);
-
-
- Assert.NotNull(stackOfBooks_Csharp);
-
- Assert.True(stackOfBooks_Csharp.Count > 0);
- }
- [Fact]
- public void Test_Different_Ways_To_Create_New_Instance_Generic_Stack()
- {
-
- Stack<Book> stackofBooks = new Stack<Book>();
-
-
- Assert.NotNull(stackofBooks);
-
-
- Assert.True(stackofBooks.Count == 0);
-
-
- var books = new Book[] {
- new Book { Id =1, Title = "C# in Depth", BookCategory = BookCategory.IT } ,
- new Book { Id =2, Title = "Pro C# 7" , BookCategory = BookCategory.IT },
- new Book { Id =3, Title = "C# 5.0 In A Nutshell", BookCategory = BookCategory.IT } };
-
-
- Stack<Book> stackOfBooks_Csharp = new Stack<Book>(books);
-
-
- Assert.NotNull(stackOfBooks_Csharp);
-
- Assert.True(stackOfBooks_Csharp.Count > 0);
- }
Adding items to stack collection.
- [Fact]
- public void Test_AddValues_To_Stack_NonGeneric()
- {
-
- Stack stackOfBooks_Csharp = new Stack();
-
- Assert.True(stackOfBooks_Csharp.Count == 0);
-
-
- stackOfBooks_Csharp.Push("C# in Depth");
- stackOfBooks_Csharp.Push("Pro C# 7");
- stackOfBooks_Csharp.Push("C# 5.0 In A Nutshell");
-
-
- Assert.True(stackOfBooks_Csharp.Count == 3);
- }
- [Fact]
- public void Test_AddValues_To_Generic_Stack()
- {
-
- Stack<Book> stackOfBooks_Csharp = new Stack<Book>();
-
- Assert.True(stackOfBooks_Csharp.Count == 0);
-
-
- stackOfBooks_Csharp.Push(new Book { Id = 1, Title = "C# in Depth", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Id = 2, Title = "Pro C# 7", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Id = 3, Title = "C# 5.0 In A Nutshell", BookCategory = BookCategory.IT });
-
-
- Assert.True(stackOfBooks_Csharp.Count == 3);
- }
Iterate through the stack collections.
- [Fact]
- public void Test_AddValues_To_Stack_And_Iterate_Through_NonGeneric()
- {
-
- Stack stackOfBooks_Csharp = new Stack();
-
-
- stackOfBooks_Csharp.Push("C# in Depth");
- stackOfBooks_Csharp.Push("Pro C# 7");
- stackOfBooks_Csharp.Push("C# 5.0 In A Nutshell");
-
-
- foreach (var item in stackOfBooks_Csharp)
- {
- Console.WriteLine(item);
-
- Assert.Contains("C#", item.ToString());
- }
- }
- [Fact]
- public void Test_AddValues_To_Stack_And_Iterate_Through_Generic()
- {
-
- Stack<Book> stackOfBooks_Csharp = new Stack<Book>();
-
-
- stackOfBooks_Csharp.Push(new Book { Id =1, Title = "C# in Depth", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Id =2, Title = "Pro C# 7", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Id =3, Title = "C# 5.0 In A Nutshell", BookCategory = BookCategory.IT });
-
-
- foreach (var item in stackOfBooks_Csharp)
- {
- Console.WriteLine(item);
-
- Assert.Contains("C#", item.ToString());
- }
- }
Get the topmost item of the stack collection.
- [Fact]
- public void Test_AddValues_To_Stack_And_Get_The_Top_Most_NonGeneric()
- {
-
- Stack stackOfBooks_Csharp = new Stack();
-
-
- stackOfBooks_Csharp.Push("C# in Depth");
- stackOfBooks_Csharp.Push("Pro C# 7");
- stackOfBooks_Csharp.Push("C# 5.0 In A Nutshell");
-
-
- object topMost = stackOfBooks_Csharp.Peek();
- string topMostInString = topMost.ToString();
-
- Assert.True(topMostInString.ToUpper() == "C# 5.0 IN A NUTSHELL");
- }
- [Fact]
- public void Test_AddValues_To_Stack_And_Get_The_Top_Most_Generic()
- {
-
- Stack<Book> stackOfBooks_Csharp = new Stack<Book>();
-
-
- stackOfBooks_Csharp.Push(new Book { Id =1,Title = "C# in Depth", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Id =2, Title = "Pro C# 7", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Id =3, Title = "C# 5.0 In A Nutshell", BookCategory = BookCategory.IT });
-
-
- Book topMost = stackOfBooks_Csharp.Peek();
- string topMostInString = topMost.ToString();
-
- Assert.True(topMostInString.ToUpper() == "C# 5.0 IN A NUTSHELL");
- }
Remove items in the stack collection.
- [Fact]
- public void Test_AddValue_To_Stack_And_Iterate_Then_Remove_Using_Pop_NonGeneric()
- {
-
- Stack stackOfBooks_Csharp = new Stack();
-
-
- stackOfBooks_Csharp.Push("C# in Depth");
- stackOfBooks_Csharp.Push("Pro C# 7");
- stackOfBooks_Csharp.Push("C# 5.0 In A Nutshell");
-
- while (stackOfBooks_Csharp.Count > 0)
- {
-
- object topMost = stackOfBooks_Csharp.Pop();
-
-
- string topMostInString = topMost.ToString();
-
- Assert.True(stackOfBooks_Csharp.Count >= 0);
- }
-
-
- Assert.True(stackOfBooks_Csharp.Count == 0);
- }
- [Fact]
- public void Test_AddValue_To_Stack_And_Iterate_Then_Remove_Using_Pop_Generic()
- {
-
- Stack<Book> stackOfBooks_Csharp = new Stack<Book>();
-
-
- stackOfBooks_Csharp.Push(new Book { Id =1, Title = "C# in Depth", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Id =2, Title = "Pro C# 7", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Id =3, Title = "C# 5.0 In A Nutshell", BookCategory = BookCategory.IT });
-
- while (stackOfBooks_Csharp.Count > 0)
- {
-
- Book topMost = stackOfBooks_Csharp.Pop();
-
-
- string topMostInString = topMost.ToString();
-
- Assert.True(stackOfBooks_Csharp.Count >= 0);
- }
-
-
- Assert.True(stackOfBooks_Csharp.Count == 0);
- }
Clear the stack collection
- [Fact]
- public void Test_AddValue_To_Stack_And_Clear_Items_NonGeneric()
- {
-
- Stack stackOfBooks_Csharp = new Stack();
-
- Assert.NotNull(stackOfBooks_Csharp);
-
-
- stackOfBooks_Csharp.Push("C# in Depth");
- stackOfBooks_Csharp.Push("Pro C# 7");
- stackOfBooks_Csharp.Push("C# 5.0 In A Nutshell");
-
- Assert.True(stackOfBooks_Csharp.Count > 0);
-
-
- stackOfBooks_Csharp.Clear();
-
- Assert.True(stackOfBooks_Csharp.Count == 0);
- }
- [Fact]
- public void Test_AddValue_To_Stack_And_Clear_Items_Generic()
- {
-
- Stack<Book> stackOfBooks_Csharp = new Stack<Book>();
-
- Assert.NotNull(stackOfBooks_Csharp);
-
-
- stackOfBooks_Csharp.Push(new Book { Title = "C# in Depth", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Title = "Pro C# 7", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Title = "C# 5.0 In A Nutshell", BookCategory = BookCategory.IT });
-
- Assert.True(stackOfBooks_Csharp.Count > 0);
-
-
- stackOfBooks_Csharp.Clear();
-
- Assert.True(stackOfBooks_Csharp.Count == 0);
- }
Check whether the stack collection contains a certain item.
- [Fact]
- public void Test_AddValue_To_Stack_And_Use_Contains_Method_NonGeneric()
- {
-
- Stack stackOfBooks_Csharp = new Stack();
-
- Assert.NotNull(stackOfBooks_Csharp);
-
-
- stackOfBooks_Csharp.Push("C# in Depth");
- stackOfBooks_Csharp.Push("Pro C# 7");
- stackOfBooks_Csharp.Push("C# 5.0 In A Nutshell");
-
-
- Assert.True(stackOfBooks_Csharp.Contains("C# in Depth"));
- Assert.False(stackOfBooks_Csharp.Contains("C#"));
- }
- [Fact]
- public void Test_AddValue_To_Stack_And_Use_Contains_Method_Generic()
- {
-
- Stack<Book> stackOfBooks_Csharp = new Stack<Book>();
-
- Assert.NotNull(stackOfBooks_Csharp);
-
-
- stackOfBooks_Csharp.Push(new Book { Title = "C# in Depth", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Title = "Pro C# 7", BookCategory = BookCategory.IT });
- stackOfBooks_Csharp.Push(new Book { Title = "C# 5.0 In A Nutshell", BookCategory = BookCategory.IT });
-
- var bookToCheck = new Book { Title = "C# in Depth", BookCategory = BookCategory.IT };
- var bookToCheck2 = new Book{ Title = "C# in Depth II", BookCategory = BookCategory.IT };
-
-
- Assert.True(stackOfBooks_Csharp.Contains(bookToCheck));
- Assert.False(stackOfBooks_Csharp.Contains(bookToCheck2));
-
- Assert.Contains(stackOfBooks_Csharp, book => book.Title == "C# in Depth");
-
- Assert.False(stackOfBooks_Csharp.Contains(bookToCheck2));
- }
Summary
In this article, we have discussed the following,
- What is a Stack Collection?
- What is the Difference between non-generic Stack to a generic Stack collection?
- Create a new instance of Stack object generic and non-generic collection
- Adding items to stack collection
- Iterate through the stack collection
- Get the topmost item of the stack collection
- Remove items in the stack collection
- Clear the stack collection
- Check whether the stack collection contains a certain item
I hope you have enjoyed this article, as I have enjoyed it while writing. You can also find the sample code here at
GitHub. Until next time, happy programming!