Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of 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. Stack<T>.Push(T) Method is used to inserts an object at the top of the Stack<T>.
- The capacity of a Stack<T> is the number of elements the Stack<T> can hold. As elements are added to a Stack<T> , 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<T> accepts null as a valid value and allows duplicate elements.
void Push(object obj);
Total number of elements in the Stack are : 1 Total number of elements in the Stack are : 2 Total number of elements in the Stack are : 3 Total number of elements in the Stack are : 4 Total number of elements in the Stack are : 5 Total number of elements in the Stack are : 6
- C# | Get object at the top of the Stack - Peek operation
- Stack.Push() Method in C#
- C# | Add an object to the end of the Queue - Enqueue Operation
- C# | Get the object at the beginning of the Queue - Peek Operation
- Removing the object from the top of the Stack in C#
- C# | Insert() Method
- C# | Insert a new entry in OrderedDictionary with specified key and value
- C# | Insert into OrderedDictionary with key and value at specified index
- C# | Insert at the specified index in StringCollection
- C# | How to insert an element in an Array?
- C# | Insert an element into the ArrayList at the specified index
- C# | Insert an element into Collection<T> at specified index
- C# | Check if an array object is equal to another array object
- C# | How to insert the elements of a collection into the List at the specified index
- C# | Bitwise OR operation between the elements of BitArray
- C# | Bitwise exclusive OR operation between the elements of BitArray
- C# Stack with Examples
- Stack.Contains() Method in C#
- C# | How to create a Stack
- Stack.Pop() Method in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.