C# Stack

In the C# programming language, the Stack<T> class is commonly utilized to push and pop elements. It uses the concept of a Stack that arranges elements in LIFO (Last In First Out) order. It can have duplicate elements. It is found in System.Collections.Generic namespace.

In C#, the Stack class is commonly utilized when we need to manage data in a LIFO structure. It enables us to perform several useful operations effectively that can help to add, remove, and view elements.

Syntax:

It has the following syntax:

Example

Stack stack_name = new Stack();

In this syntax,

  • stack_name: It represents the name of the stack.
  • Key Operations of the C# Stack Class

The C# stack class allows us to perform several operations. Some of them are as follows:

1) Push(T item) Function

In the C# programming language, the Push (T item) method is commonly utilized to insert an item onto the top of the stack. It follows the LIFO (Last-In-First-Out) principle, which means that the most recently added item in the stack will be removed first.

C# Stack Class Example Using push method

Let us take an example to illustrate the stack class using the push method in C#.

Example

Example

using System;

using System.Collections.Generic;

class C# Tutorial

{

    static void Main()

    {

        Stack<string> stack = new Stack<string>();

        stack.Push("Apple");

        stack.Push("Banana");

        stack.Push("Cherry");

        Console.WriteLine("Elements after Push:");

        foreach (var i in stack)

            Console.WriteLine(i);

    }

}

Output:

Output

Elements after Push:

Cherry

Banana

Apple

Explanation:

In this example, we create a generic stack of strings that pushes three items ("Apple", "Banana", "Cherry") onto it, and prints the stack. Because a stack operates in LIFO order, the last pushed item ("Cherry") is printed first in the output, which is followed by "Banana" and then "Apple".

2) Pop Function

In the C# programming language, the pop function is commonly utilized to remove and return the item from the top of the stack.

C# Stack Class Example using the pop function

Let's consider an example to demonstrate the stack class using the pop function in C#.

Example

Example

using System;

using System.Collections.Generic;

class pop

{

    static void Main()

    {

        Stack<int> s = new Stack<int>();

        s.Push(100);

        s.Push(200);

        s.Push(300);

        Console.WriteLine("Popped element: " + s.Pop());

        Console.WriteLine("Remaining elements:");

        foreach (var j in s)

            Console.WriteLine(j);

    }

}

Output:

Output

Popped element: 300

Remaining elements:

200

100

Output:

In this example, we create an integer stack, where we insert 100, 200, and 300 into it. After that, it deletes and prints the top value with Pop (which returns 300). Finally, it prints the rest of the elements, which display 200 and 100 in LIFO fashion.

3) Peek Function

In the C# programming language, it is commonly utilized to return the item at the top of the stack, but it does not remove it.

C# Stack class Example using the peek function

Let us consider a simple example to demonstrate the stack class using the peek function in C#.

Example

Example

using System;

using System.Collections.Generic;

class C# Tutorial

{

    static void Main()

    {

        Stack<string> stack = new Stack<string>();

        stack.Push("Red");

        stack.Push("Green");

        stack.Push("Blue");

        Console.WriteLine("Top element: " + stack.Peek());

        Console.WriteLine("Stack after Peek:");

        foreach (var item in stack)

            Console.WriteLine(item);

    }

}

Output:

Output

Top element: Blue

Stack after Peek (unchanged):

Blue

Green

Red

Explanation:

In this example, we have created a stack of strings that pushes "Red", "Green", and "Blue" into the stack, and then we use the Peek function to display the top item ("Blue") without popping it. After that, it prints the contents of the stack, which do not change: Blue, Green, Red.

4) Contains(T item) Function

In the C# programming language, the contains(T item) function is commonly utilized to determine whether a specific element exists within the stack. It searches the stack for the specified value. If the element is present in the stack, it returns a true value; otherwise, it returns a false value.

C# Stack class Example using the contains function

Let's take an example to demonstrate the stack class using the contains function in C#.

Example

Example

using System;

using System.Collections.Generic;

class stack

{

    static void Main()

    {

        Stack<int> st = new Stack<int>();

        st.Push(5);

        st.Push(10);

        st.Push(15);

        Console.WriteLine("Does the stack contain 10 in it: " + st.Contains(10));

        Console.WriteLine("Does the stack contain 20 in it: " + st.Contains(20));

    }

}

Output:

Output

Does the stack contain 10 in it: True

Does the stack contain 20 in it: False

Explanation:

In this example, we create a stack of integers that pushes elements 5, 10, and 15 onto it. After that, we use the Contains function to check whether 10 and 20 are in the stack. It outputs True for 10 because it is there, and False for 20 because it is not.

5) Count Function

The count function in the C# programming language is commonly utilized to get the number of elements in the stack. In simple words, it is used to calculate the total no of elements in the stack.

C# Stack class Example using the count function

Let us take an example to demonstrate the stack class using the count function in C#.

Example

Example

using System;

using System.Collections.Generic;

class counting

{

    static void Main()

    {

        Stack<char> stack = new Stack<char>();

        stack.Push('A');

        stack.Push('B');

        stack.Push('C');

        stack.Push('D');

        Console.WriteLine("Total elements in stack: " + stack.Count);

    }

}

Output:

Output

Total elements in stack: 4

Explanation:

In this example, we have created a stack of characters that pushes 'A', 'B', 'C', and 'D' onto it. After that, it prints the number of elements using the Count function, which is 4.

6) Clear Function

In the C# programming language, the clear function is commonly utilized to clear all items from the stack.

C# Stack class Example using the clear function

Let's consider a simple example to illustrate the stack class using the clear function in C#.

Example

Example

using System;

using System.Collections.Generic;

class C# Tutorial

{

    static void Main()

    {

        Stack<string> stack = new Stack<string>();

        stack.Push("a");

        stack.Push("b");

        stack.Push("c");

        Console.WriteLine("Count before Clear: " + stack.Count);

        stack.Clear();

        Console.WriteLine("Count after Clear: " + stack.Count);

    }

}

Output:

Output

Count before Clear: 3

Count after Clear: 0

Explanation:

In this example, we have created a stack of strings taht pushes "a", "b", and "c" onto it. After that, we use the count function to display the count of elements of the stack before clearing it as 3. Next, we delete all elements using the Clear function, and again we count the total elements in the stack using the count function, which is now 0.

7) ToArray Function

In the C# programming language, the ToArray function is commonly utilized to copy the elements of the stack into a new array.

C# Stack class Example using the ToArray function

Let us take an example to demonstrate the stack class using the ToArray function in C#.

Example

Example

using System;

using System.Collections.Generic;

class C# Tutorial

{

    static void Main()

    {

        Stack<int> stack = new Stack<int>();

        stack.Push(1);

        stack.Push(2);

        stack.Push(3);

        int[] arr = stack.ToArray();

        Console.WriteLine("Array elements:");

        foreach (int n in arr)

            Console.WriteLine(n);

    }

}

Output:

Output

Array elements:

3

2

1

Explanation:

In this example, we have created a stack of integers that pushes 1, 2, and 3 onto it. After that, we use the ToArray function to copy the stack to an array. Next, it prints the array elements in LIFO fashion, which is displayed as 3, 2, 1.

C# Stack class Example using the Push, Pop, and Peek Operations

Let's see an example of a generic Stack<T> class that stores elements using the Push method, removes elements using the Pop method, and iterates elements using a for-each loop.

Example

Example

using System;  

using System.Collections.Generic;    

public class StackExample  

{  

    public static void Main(string[] args)  

    {  

        Stack<string> names = new Stack<string>();  

        names.Push("Albert");  

        names.Push("Peter");  

        names.Push("James");  

        names.Push("Michael");  

        names.Push("Robert");    

        foreach (string name in names)  

        {  

            Console.WriteLine(name);  

        }    

        Console.WriteLine("Peek element: "+names.Peek());  

        Console.WriteLine("Pop: "+ names.Pop());  

        Console.WriteLine("After Pop, Peek element: " + names.Peek());  

  

    }  

}

Output:

Output

Robert

Michael

James

Peter

Albert

Peek element: Robert

Pop: Robert

After Pop, Peek element: Michael

Explanation:

In this example, we have created a Stack of strings named names that includes several elements using the Push method. The elements are displayed using a foreach loop, which shows the Last-In-First-Out (LIFO) order. After that, the Peek method retrieves the top element without removing it, while the Pop method removes the top element from the stack. Finally, we print the updated top element after performing the pop operation.

Features of the Stack<T> class in C#

There are several features of the stack class in C#. Some of them are as follows:

  • In the C# stack class, we can include the elements using the Push method.
  • The top element can be retrieved and popped through Pop, or simply retrieved without removal through Peek.
  • It is not indexable.
  • It supports null (for reference types) and duplicate items.
  • We can have an empty stack or initialize it from a collection or an array.
  • The Stack<T> class grows dynamically as elements are pushed, but also has a capacity property to allow for optimization of memory.
  • Conclusion

In conclusion, the Stack class in C# is an effective class that uses the Last In, First Out (LIFO) principle, which makes it a better choice for several scenarios, such as reversing data, expression evaluation, and undo operations. It allows us to perform several operations, such as Push, Pop, and Peek, with type safety and fast performance. However, it gives access only to the top element. Overall, the Stack gives a simple and effective method to handle the temporary or sequential data in C#.

C# Stack Class FAQs

1) How do we create a Stack in C#?

We can use the following syntax to create a stack in C#:

Generic stack:

Example

Stack<int> stack = new Stack<int>();

Non-generic stack:

Example

Stack stack = new Stack();.

2) What are the time complexities of stack operations in C#?

In the C# programming language, the time complexity for the stack operations, such as Push, Pop, and Peek function, is constant, i.e., O(1).

3) How to check the number of elements in a Stack in C#?

We can use the Count property to get the total elements in the stack in C#.

4) Can we iterate through a Stack in C#?

Yes, we can iterate using a foreach loop that will enumerate elements in LIFO order.

5) When to use a Stack in C# programming?

It is suitable when we need LIFO access, like undo functionality, parsing expressions, or backtracking algorithms.

Input Required

This code uses input(). Please provide values below: