Stack.Push() Method in C#
This method(comes under System.Collections namespace) is used to inserts an object at the top of the Stack. If the Count already equals the capacity, the capacity of the Stack is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added. 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.
Syntax:
public virtual void Push (object obj);
Example:
// C# code to demonstrate the // Stack.Push() Method using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Stack Stack myStack = new Stack(); // Inserting the elements into the Stack myStack.Push("one"); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements "+ "in the Stack are : "); Console.WriteLine(myStack.Count); myStack.Push("two"); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements"+ " in the Stack are : "); Console.WriteLine(myStack.Count); myStack.Push("three"); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements"+ " in the Stack are : "); Console.WriteLine(myStack.Count); myStack.Push("four"); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements"+ " in the Stack are : "); Console.WriteLine(myStack.Count); myStack.Push("five"); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements"+ " in the Stack are : "); Console.WriteLine(myStack.Count); myStack.Push("six"); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements"+ " in the Stack are : "); Console.WriteLine(myStack.Count); } } |
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
Reference:
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | String.Contains() Method
- C# | Math.Max() Method
- Decimal.Add() Method in C#
- C# | Math.Min() Method
- Stack.Pop() Method in C#
- DateTimeOffset.Add() Method in C#
- Extension Method in C#
- Stack.Contains() Method in C#
- C# | CopyTo() Method
- C# | Trim() Method
- C# | Method Parameters
- C# | Remove() Method
- C# | IndexOfAny() Method
- Queue.Contains() 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 contribute@geeksforgeeks.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.



