C# Stack - Push() Method



The C# stack Push() method is used to insert an object or item at the top of the stack. When the count equals the capacity, the stack's capacity is increased by automatically reallocating the internal array, and the old elements are transferred to the new array before inserting the new element.

If the count is less than the stack's capacity, then push is an O(1) operation. If the capacity needs to be increased to insert the new element, push becomes an O(n) operation. Where n is the number of elements or count.

Syntax

Following is the syntax of the C# stack Push() method −

public virtual void Push (object obj);

Parameters

This method accepts an object as a parameter, where an object represents an element pushed onto the stack. it can be null −

Return value

This method does not return any value.

Example 1: Using the Push Method in Stack<int>

Following is the basic example of the Push() method to insert an element into the stack −

    
using System;
using System.Collections.Generic;
class Example {
   static void Main() {
      Stack<int> numbers = new Stack<int>();
      
      // inserting elements
      numbers.Push(10);
      numbers.Push(20);
      numbers.Push(30);

      Console.WriteLine("Stack after Push operations:");
      foreach (int num in numbers) {
	     Console.WriteLine(num);
      }
   }
}

Output

Following is the output −

Stack after Push operations:
30
20
10

Example 2: Using the Push Method in Stack<string>

Let's see another example of the Push() method to insert string element in the stack −

using System;
using System.Collections.Generic;
class Example {
   static void Main() {
      Stack<string> words = new Stack<string>();

      words.Push("Hello");
      words.Push("World");
      words.Push("C#");

      Console.WriteLine("Stack after Push operations:");
      foreach (string word in words) {
         Console.WriteLine(word);
      }
   }
}

Output

Following is the output −

Stack after Push operations:
C#
World
Hello

Example 3: Using Push for Mixed Data Types in Stack

The below example, uses the push() method to store different types of element in the stack −

using System;
using System.Collections;

class Example {
   static void Main() {
      Stack myStack = new Stack();
      myStack.Push(100);
      myStack.Push("C#");
      myStack.Push(3.14);
      myStack.Push(true);
      
      Console.WriteLine("Stack after Push operations:");
      foreach (var item in myStack) {
          Console.WriteLine(item);
      }
   }
}

Output

Following is the output −

Stack after Push operations:
True
3.14
C#
100

Example 4: Using Push Method for Storing Mixed Data Types in Stack

Here, in this example, we use the push method to store custom objects in the stack −

using System;
using System.Collections.Generic;

class Person {
   public string Name { get; set; }
   public int Age { get; set; }

   public Person(string name, int age) {
      Name = name;
      Age = age;
   }

   public override string ToString() {
      return $"Name: {Name}, Age: {Age}";
   }
}

class Example {
   static void Main() {
      Stack<Person> people = new Stack<Person>();

      people.Push(new Person("Aman", 25));
      people.Push(new Person("Gupta", 24));
      people.Push(new Person("Vivek", 26));

      Console.WriteLine("Stack after Push operations:");
      foreach (var person in people) {
         Console.WriteLine(person);
      }
   }
}

Output

Following is the output −

Stack after Push operations:
Name: Vivek, Age: 26
Name: Gupta, Age: 24
Name: Aman, Age: 25
csharp_stack.htm
Advertisements