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.
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.