Stack.Push() Method in C# (original) (raw)
Last Updated : 04 Feb, 2019
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:
CSHARP `
// 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);
}
}
`
Output:
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:
Similar Reads
- C# Stack Class In C#, the Stack class represents a Last-in-First-out (LIFO) collection of objects. The stack is the part of the System.Collections.Generic namespace. This class allows us to push elements onto the stack, pop elements from the stack, and peek at the top element without removing it.The capac 5 min read
- C# | How to create a Stack Stack() constructor is used to initialize a new instance of the Stack class which will be empty and will have the default initial capacity. Stack represents a last-in, first out collection of object. It is used when you need last-in, first-out access to items. When you add an item in the list, it is 2 min read
- Stack.Count Property in C# This method(comes under System.Collections namespace) is used to get the number of elements contained in the Stack. The capacity is the number of elements that the Stack can store and the count is the number of elements that are actually in the Stack. The capacity is always greater than or equal to 2 min read
- Stack.IsSynchronized Property in C# This method(comes under System.Collections namespace) is used to get a value indicating whether access to the Stack is synchronized (thread safe) or not. To guarantee the thread safety of the Stack, all operations must be done through the wrapper returned by the Synchronized method. Also, retrieving 2 min read
- How to get Synchronize access to the Stack in C# Stack.SyncRoot Property is used to get an object which can be used to synchronize access to the Stack. Stack represents last-in, first out collection of object. It is used when you need last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you r 3 min read
- Stack.Clear Method in C# This method(comes under System.Collections namespace) is used to remove all the objects from the Stack. This method will set the Count of Stack to zero, and references to other objects from elements of the collection are also removed. This method is an O(n) operation, where n is Count. Syntax: publi 2 min read
- Stack.Clone() Method in C# This method is used to create a shallow copy of the Stack. It just creates a copy of the Stack. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array. Syntax: public virtual object Clone (); Return Value: The method returns an Ob 2 min read
- Stack.Contains() Method in C# This method(comes under System.Collections namespace) is used to check whether a specified element is present is Stack or not. Internally this method checks for equality by calling the Object.Equals method. Also, it performs a linear search, therefore, this method is an O(n) operation, where n is Co 2 min read
- Stack.CopyTo() Method in C# This method(comes under System.Collections namespace) is used to copy the Stack to an existing 1-D Array which starts from the specified array index. The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to Po 2 min read
- Stack.Equals() Method in C# Equals(Object) Method which is inherited from the Object class is used to check if a specified Stack class object is equal to another Stack class object or not. This method comes under the System.Collections namespace. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is 2 min read