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. Creation of stack means the addition of item into the stack. Stack<T>.Push(Object) Method is used to Inserts an object at the top of the Stack.
- 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.
public virtual void Push (object obj);
obj: The Object of type System.Object which is to push onto the Stack<T>. The value can be null.
Below given are some examples to understand the implementation in a better way :
Total number of elements in the Stack 1 are : 5 website best the is GeeksforGeeks Total number of elements in the Stack 2 are : 5 GeeksforGeeks is the best website
Total number of elements in the Stack 1 are : 5 25 20 15 10 5 Total number of elements in the Stack 2 are : 5 5 10 15 20 25
- C# | Create a Queue from another collection
- C# | Create HashSet from another collection
- C# | How to create a Stack
- C# | Add an object to the end of Collection<T>
- C# | Collection Class
- C# | Add key and value into OrderedDictionary collection
- C# | Get an enumerator that iterates through Collection<T>
- Object and Collection Initializer in C#
- Garbage Collection in C# | .NET Framework
- C# | Get a collection of values in the StringDictionary
- C# | Union of SortedSet to a collection
- C# | Remove all elements from the Collection<T>
- C# | Intersection of SortedSet with a collection
- C# | Get a collection of keys in the StringDictionary
- C# | Get or set the element at specified index in Collection<T>
- C# | Check if an element is in the Collection<T>
- C# | Check if SortedSet and the specified collection contain the same elements
- C# | Check if a HashSet is a superset of the specified collection
- C# | Check if a SortedSet is a subset of the specified collection
- C# | Remove all elements in a collection from a HashSet
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.