C#
C# Array Tutorial: Create, Declare, Initialize
What is an Arrays in C#? An array is used to store a collection or series of elements. These...
The stack is a special case collection which represents a last in first out (LIFO) concept. To first understand LIFO, let's take an example. Imagine a stack of books with each book kept on top of each other.
The concept of last in first out in the case of books means that only the top most book can be removed from the stack of books. It is not possible to remove a book from between, because then that would disturb the setting of the stack.
Hence in C#, the stack also works in the same way. Elements are added to the stack, one on the top of each other. The process of adding an element to the stack is called a push operation. To remove an element from a stack, you can also remove the top most element of the stack. This operation is known as pop.
Let's look at the operations available for the Stack collection in more detail.
A stack is created with the help of the Stack Data type. The keyword "new" is used to create an object of a Stack. The object is then assigned to the variable st.
Stack st = new Stack()
The push method is used to add an element onto the stack. The general syntax of the statement is given below.
Stack.push(element)
The pop method is used to remove an element from the stack. The pop operation will return the topmost element of the stack. The general syntax of the statement is given below
Stack.pop()
This property is used to get the number of items in the Stack. Below is the general syntax of this statement.
Stack.Count
This method is used to see if an element is present in the Stack. Below is the general syntax of this statement. The statement will return true if the element exists, else it will return the value false.
Stack.Contains(element)
Now let's see this working at a code level. All of the below-mentioned code will be written to our Console application. The code will be written to our Program.cs file.
In the below program, we will write the code to see how we can use the above-mentioned methods.
In this example, we will see
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Stack st = new Stack();
st.Push(1);
st.Push(2);
st.Push(3);
foreach (Object obj in st)
{
Console.WriteLine(obj);
}
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("The number of elements in the stack " +st.Count);
Console.WriteLine("Does the stack contain the elements 3 "+st.Contains(3));
Console.ReadKey();
}
}
}Code Explanation:-
If the above code is entered properly and the program is run the following output will be displayed.
Output:
From the output, we can see that the elements of the stack are displayed. Also, the value of True is displayed to say that the value of 3 is defined on the stack.
Note: You have noticed that the last element pushed onto the stack is displayed first. This is the topmost element of the stack. The count of stack elements is also shown in the output.
Now let's look at the "remove" functionality. We will see the code required to remove the topmost element from the stack.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Stack st = new Stack();
st.Push(1);
st.Push(2);
st.Push(3);
st.Pop();
foreach (Object obj in st)
{
Console.WriteLine(obj);
}
Console.ReadKey();
}
}
}Code Explanation:-
If the above code is entered properly and the program is run, the following output will be displayed.
Output:
We can see that the element 3 was removed from the stack.
Summary
What is an Arrays in C#? An array is used to store a collection or series of elements. These...
In C# file operations, normally streams are used to read and write to files. A stream is an...
The concept of Serialization and deserialization is used whenever data pertaining to objects have...
Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It is used...
What is ArrayList in C#? The ArrayList collection is similar to the Arrays data type in C#. The...
In this tutorial, you will learn- Inheritance Polymorphism What is Inheritance in C#? Inheritance is...