![]() |
VOOZH | about |
A Stack<T> in C# is a collection that stores elements in LIFO (Last In, First Out) order. This means the last element inserted is the first one removed. It is defined in the System.Collections.Generic namespace and provides fast insertion and removal from the top of the stack.
Example: This example demonstrates how to use Stack to push and pop elements in LIFO order.
4 3 2 1
1. Generic Stack: Holds elements of a specific type.
Stack<T> stackName = new Stack<T>();
2. Non-Generic Stack: Can hold elements of any type but requires casting when retrieving elements.
Stack stack = new Stack(); // Non-generic stack
Example: Example demonstrating both generic and non-generic Stack usage
Non-Generic Stack: Geeks123 2.5 1 GFG Generic Stack: Stack Example GeeksForGeeks GFG
The Stack<T> class provides three constructor which are listed below in the table:
Constructor | Description |
|---|---|
Initializes an empty stack with the default initial capacity. | |
Stack(ICollection) | Initializes a new stack containing elements copied from a specified collection. |
Stack(Int32) | Initializes an empty stack with the specified initial capacity (or the default if the given capacity is smaller). |
Example: This example demonstrates the basic operations of Stack such as adding an element, checking the Stack size and viewing the top element without modify the stack.
Total elements in Stack: 6 Top element is: 60 Top element again is: 60 Updated count of elements: 6
The Stack<T> class provides several properties to access its state.
Property | Description |
|---|---|
Returns the number of elements currently in the stack. | |
Indicates whether the stack is thread-safe. | |
Provides an object to synchronize access to the stack. |
Example: This example demonstrates the total number of elements in the Stack.
Total number of elements in the Stack are: 6
| Method | Description |
|---|---|
| Clear() | Removes all objects from the Stack. |
| Clone() | Creates a shallow copy of the Stack. |
| Contains(Object) | Determines whether an element is in the Stack. |
| CopyTo(Array, Int32) | Copies the Stack to an existing one-dimensional Array, starting at the specified array index. |
| Equals(Object) | Determines whether the specified object is equal to the current object. |
| GetEnumerator() | Returns an IEnumerator for the Stack. |
| GetHashCode() | Serves as the default hash function. |
| GetType() | Gets the Type of the current instance. |
| MemberwiseClone() | Creates a shallow copy of the current Object. |
| Peek() | Returns the object at the top of the Stack without removing it. |
| Pop() | Removes and returns the object at the top of the Stack. |
| Push(Object) | Inserts an object at the top of the Stack. |
| Synchronized(Stack) | Returns a synchronized (thread safe) wrapper for the Stack. |
| ToArray() | Copies the Stack to a new array. |
| ToString() | Returns a string that represents the current object. |
Example: This example demonstrates how to use Clear() to remove all the elements from the stack.
Total elements in Stack before clear: 6 Total elements in Stack after clear: 0
Example: This example demonstrates how to check if a specific element is present in a stack using the Contains().
The element Geek2 is present? :True The element Geek10 is present? :False