Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
C# Stack Class
3.
Characteristics of a Stack Class
4.
Constructors
5.
Properties
6.
Methods 
7.
Examples
7.1.
Code 1
7.2.
Code 2
7.3.
Code 3
8.
FAQs
8.1.
What is a Stack Class?
8.2.
Give an example of a Stack.
8.3.
How is the Stack helpful?
9.
Conclusion
Last Updated: Mar 27, 2024

C# Stack Class

Introduction

What is Stack is actually? A stack is a last-in, first-out collection of objects. It is used when items must be obtained last-in-first-out. When an item is added to the list, it is regarded as pushing the thing, and removing it is referred to as popping the item. This article will learn about the C# Stack Class and its characteristics, constructors, properties, and methods. It should also include different code examples.

C# Stack Class

A stack is a last-in, first-out collection of objects. It is used when items must be accessed last-in-first-out. Once an item is added to the list, it is referred to as pushing the item, and removing it is referred to as popping the item. This class is part of the System.Collections namespace.

Characteristics of a Stack Class

  • A Stack's capacity is the number of elements it can hold. As elements are inserted into a Stack, the capacity is automatically increased through reallocation as needed.
  • If the count is less than the Stack's capacity, a push is an O(1) operation. Push becomes an O(n) operation if the capacity needs to be increased to accommodate the new element. Pop is a one-of-a-kind operation.
  • Stack allows duplicate elements and accepts null as a valid value.

Constructors

Properties

Methods 

Examples

Let us see a few examples of C# Stack class.

Code 1

using System;
using System.Collections;


class StackCSharp {


public static void Main()
{
Stack myStack = new Stack();


myStack.Push(" Element One");
myStack.Push(" Element Two");
myStack.Push(" Element Three");
myStack.Push(" Element Four");
myStack.Push(" Element Five");


Console.Write("Elements in the Stack are: ");


Console.WriteLine(myStack.Count);


myStack.Clear();


Console.Write(" Elements in the Stack are: ");


Console.WriteLine(myStack.Count);
}
}

Output

Elements in the Stack are: 5
Elements in the Stack are: 0

In this example, we have added the elements and shown the output, and after emptying the Stack, we also have shown the output.

Code 2

using System;
using System.Collections;


class StackCSharpDemo {


public static void Main()
{


Stack myStack = new Stack();


myStack.Push("Coding");
myStack.Push("Ninja Classes");
myStack.Push("Data Structures");
myStack.Push("Algorithms");


Console.WriteLine(myStack.Contains("Ninja Classes"));
}
}

Output

True

In this example, we have added elements in the Stack, and we also checked whether particular elements exist or not.

Code 3

using System;
using System.Collections;
public class SamplesStackInCSharp {


   public static void Main() {


      Stack myStack = new Stack();
      myStack.Push("Hello");
      myStack.Push("Ninjas");
      myStack.Push("!");
      
      Console.WriteLine( "Count: {0}", myStack.Count );
      Console.Write( "Values:" );
      PrintValues( myStack );
   }


   public static void PrintValues( IEnumerable myCollection ) {
      foreach ( Object obj in myCollection )
         Console.Write( " {0}", obj );
      Console.WriteLine();
   }
}

Output

Count: 3
Values: ! Ninjas Hello

In this example, we have added the elements, and after adding them, we have shown how many elements and what are the elements we have added.

Must Read IEnumerable vs IQueryable.

FAQs

What is a Stack Class?

A stack is a collection of objects that are ordered from last in to first out. When you need last-in, first-out access to items, this is the method to utilise. Pushing an item into the list is referred to as pushing the item, and popping the item is referred to as popping the item. The namespace for this class is System.Collections.

Give an example of a Stack.

A stack of plates is a real-world example: you can only take a plate from the top of the Stack and only add a plate to the top of the Stack.

How is the Stack helpful?

You can use a stack to store temporary data in a LIFO fashion, and you may want to remove an element after getting its value.

Conclusion

In this article, we have covered the topic of the C# Stack class. We introduced the topic. We also discussed its characteristics along with its properties. We also discussed its methods and examples.

With our Coding Ninjas Studio Guided Path, you may learn about  Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more. Check out the mock test series and the competitions on Coding Ninjas Studio if you want to put your coding talents to the test! Check out the problemsinterview experiences, and interview bundle for placement preparations if you're just getting started and want to see what questions large companies like Amazon, Microsoft, and Uber ask.

We hope this blog has aided you in broadening your horizons. Please give this article a thumbs up if you enjoyed reading it .

"Have fun coding!"

Live masterclass