Junior Collections

What is a Stack<T> in C#?

A Stack<T> is a generic collection in C# that stores elements in a Last-In, First-Out

(LIFO) order.

  • The last element added is the first one removed
  • Belongs to System.Collections.Generic

Real-world use case:

Undo operations, browser history, expression evaluation.

Example:

Stack<int> numbers = new Stack<int>();

numbers.Push(10);

numbers.Push(20); // Top of the stack

More from C# Programming Tutorial

All questions for this course