MSDN Documentation

.NET Framework Collections

System.Collections.Generic.Stack<T> Class

Represents a collection of objects that can be accessed by the last object that was added. The Stack<T> class provides LIFO (Last-In, First-Out) behavior.

Inheritance Hierarchy

System.Object > System.Collections.Generic.Stack<T>

Syntax

public class Stack<T> : IEnumerable<T>, IEnumerable

Remarks

A Stack<T> is a generic collection that supports the LIFO principle. This means that you can only access the last element that was added to the stack. You can push an element onto the top of the stack and pop an element off the top of the stack.

The Stack<T> class is a good choice for scenarios where you need to store a collection of items and retrieve them in the reverse order of their addition. Examples include:

The Stack<T> collection is not synchronized. It is not thread-safe.

Constructors

Name Description
Stack() Initializes a new instance of the Stack<T> class that is empty, has the default initial capacity, and uses the default equality comparer for the type of elements that the collection contains.
Stack(int capacity) Initializes a new instance of the Stack<T> class that is empty, has the specified initial capacity, and uses the default equality comparer for the type of elements that the collection contains.
Stack(IEnumerable<T> collection) Initializes a new instance of the Stack<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.

Methods

Push(T item)

public void Push(T item)

Adds an object to the top of the Stack<T>.

Pop()

public T Pop()

Removes and returns the object at the top of the Stack<T>. Throws an InvalidOperationException if the stack is empty.

Peek()

public T Peek()

Returns the object at the top of the Stack<T> without removing it. Throws an InvalidOperationException if the stack is empty.

Clear()

public void Clear()

Removes all objects from the Stack<T>.

Contains(T item)

public bool Contains(T item)

Determines whether an element is in the Stack<T>.

CopyTo(T[] array, int arrayIndex)

public void CopyTo(T[] array, int arrayIndex)

Copies the Stack<T> elements to a new array, starting at the specified array index and including all elements that are in the order they are in the Stack<T>.

GetEnumerator()

public IEnumerator<T> GetEnumerator()

Returns an enumerator that iterates through the Stack<T>.

Properties

Count

public int Count { get; }

Gets the number of elements contained in the Stack<T>.

Example Usage

The following example demonstrates how to use the Stack<T> class to store and retrieve strings:

using System;
using System.Collections.Generic;

public class StackExample
{
    public static void Main(string[] args)
    {
        // Create a new stack of strings
        Stack<string> myStack = new Stack<string>();

        // Push elements onto the stack
        myStack.Push("Hello");
        myStack.Push("World");
        myStack.Push("!");

        Console.WriteLine($"Stack count: {myStack.Count}"); // Output: Stack count: 3

        // Peek at the top element
        Console.WriteLine($"Top element: {myStack.Peek()}"); // Output: Top element: !

        // Pop elements from the stack
        while (myStack.Count > 0)
        {
            Console.WriteLine($"Popped: {myStack.Pop()}");
        }
        // Output:
        // Popped: !
        // Popped: World
        // Popped: Hello

        Console.WriteLine($"Stack count after popping: {myStack.Count}"); // Output: Stack count after popping: 0
    }
}
Note: The Stack<T> iterates over elements in LIFO order. The first element returned by the enumerator is the last element pushed onto the stack.