Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. This class comes under System.Collections namespace.
Characteristics of Stack Class:
- The capacity of a Stack is the number of elements the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.
- If Count is less than the capacity of the stack, Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count. Pop is an O(1) operation.
- Stack accepts null as a valid value and allows duplicate elements.
|Stack()||Initializes a new instance of the Stack class that is empty and has the default initial capacity.|
|Stack(ICollection)||Initializes a new instance of the Stack class that contains elements copied from the specified collection and has the same initial capacity as the number of elements copied.|
|Stack(Int32)||Initializes a new instance of the Stack class that is empty and has the specified initial capacity or the default initial capacity, whichever is greater.|
Total number of elements in the Stack are : 6 Element at the top is : 6th Element Element at the top is : 6th Element Total number of elements in the Stack are : 6
|Count||Gets the number of elements contained in the Stack.|
|IsSynchronized||Gets a value indicating whether access to the Stack is synchronized (thread safe).|
|SyncRoot||Gets an object that can be used to synchronize access to the Stack.|
Total number of elements in the Stack are : 6
|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.|
Total number of elements in the Stack are : 6 Total number of elements in the Stack are : 0
This article is attributed to GeeksforGeeks.org