C# Stack Push() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Stack

Definition

In C#, Stack.Push(item) puts one item on top of a Stack<T>. A stack is last in, first out (LIFO): the newest item is always the first to come off, and older items wait underneath it.

Think of a pile of plates in a kitchen. A clean plate goes on top of the pile, and nobody pulls one out of the middle. Push is the "put a plate on" step. The matching "take a plate off" step is Pop(). If you want a line where the oldest item leaves first, use a Queue<T> instead.

This is an instance member of Stack<T> (System.Collections.Generic). Call it on a stack variable, such as history.Push(...). A Stack<T> is a class, so two variables can share one stack. It is last in, first out (LIFO): the newest item sits on top and leaves first. It is not thread-safe; for several threads use ConcurrentStack<T>.

Syntax

Call Push with one item. It returns nothing, so there is no result to check.

C#
Stack<string> plates = new Stack<string>();
plates.Push("white");      // stack: white
plates.Push("blue");       // stack: blue on top of white
int height = plates.Count; // 2
CallWhat it does
plates.Push("white")Puts "white" on top. Count goes up by 1.
plates.Push("white") (again)Allowed. A stack keeps duplicates, so the same value can sit in the pile twice.
plates.Push(null)Allowed for reference types such as string. The stack stores the null like any other item.
plates.Pop()The other side: take the top item off. See Pop().

Push is O(1) on average. When the internal array is full, the stack allocates a bigger one and copies the items, which takes more time, but this happens rarely. If you know how many items are coming, EnsureCapacity() can avoid that growing. A Stack<T> has no indexer, so you cannot push into the middle.

Library declarations
C#
public void Push(T item);

Parameters

ParameterDescription
itemThe item to put on top. For a reference type such as string it may be null. Repeated values are fine.

Return Value

Return ValueDescription
voidNothing is returned. After the call, Count is 1 higher and the new item is the top of the stack.

C# Examples

Example 1 — Stack Three Plates

Push three colors, then check the height of the pile, the top plate, and the order a foreach prints them.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Stack<string> plates = new Stack<string>();

        plates.Push("white");
        plates.Push("blue");
        plates.Push("red");

        Console.WriteLine(plates.Count);
        Console.WriteLine(plates.Peek());
        Console.WriteLine(string.Join(", ", plates));
    }
}

How It Works

  1. Starts empty. new Stack<string>() holds nothing. Each Push call adds one color on top of the pile.
  2. The height shows up in Count. Push returns nothing, so Count (now 3) is how you see that the calls worked.
  3. Newest is on top. Red was pushed last, so Peek() reports "red". Pushing never pulls an older plate to the top.
  4. Printed top to bottom. string.Join walks the stack from the top down, so the line is the reverse of the push order: red, blue, white.

Example 2 — Duplicates and null Are Allowed

Push the same word twice and a null, then pop them to see that all three were kept.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Stack<string> words = new Stack<string>();

        words.Push("again");
        words.Push("again");
        words.Push(null);

        Console.WriteLine(words.Count);
        Console.WriteLine(words.Pop() == null);
        Console.WriteLine(words.Pop());
        Console.WriteLine(words.Count);
    }
}

How It Works

  1. No duplicate rule. A stack never refuses an item, unlike a set. Both "again" values are stored, and the null too, so Count is 3.
  2. null is a normal item. For a reference type, Push(null) stores a real slot. (With nullable warnings on, declare the stack as Stack<string?>.)
  3. The last push leaves first. The first Pop() returns the null because it was pushed last, and == null prints True.
  4. Then the copies. The next Pop() returns "again". One duplicate is still inside, so the final Count is 1.

Example 3 — Reverse a Word With a Stack

Push every letter of a word, then pop them all. Letters come off in the opposite order, so the word is reversed.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        string word = "stack";
        Stack<char> letters = new Stack<char>();

        foreach (char c in word) {
            letters.Push(c);
        }

        string reversed = "";
        while (letters.Count > 0) {
            reversed += letters.Pop();
        }

        Console.WriteLine(reversed);
        Console.WriteLine(letters.Count);
    }
}

How It Works

  1. One letter at a time. The foreach pushes s, t, a, c, k in that order, so k ends up on top.
  2. A stack of char. Stack<char> works just like a stack of strings. Push accepts any value of the item type.
  3. Popping flips the order. The loop takes k first and s last, and glues each letter onto reversed.
  4. Nothing left. The loop stops when Count is 0, so the output is kcats and the stack is empty.

Frequently Asked Questions

It puts one item on top of the Stack<T>. The item will be the first one removed by Pop, and Count goes up by 1.
No. It returns void. Check the Count property if you want to see how many items are in the stack.
Yes. A stack keeps duplicate values. null is allowed when the item type is a reference type such as string. For a value type such as int there is no null.
On top. The item pushed last is the first to come back out, because a stack is last in, first out.
No. If several threads push and pop at the same time, use ConcurrentStack<T> from System.Collections.Concurrent, or protect the stack with a lock.

Next: ToArray()

Copy the stack into a new array, top to bottom. The stack itself is not changed.

ToArray() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful