Read the syntax
Queue<T> is first-in, first-out: Enqueue adds at the back and Dequeue removes from the front. Stack<T> is last-in, first-out: Push adds at the top and Pop removes the top.
Peek observes the next item without removing it. Check Count before Dequeue, Pop or Peek when an empty collection is possible; those methods throw on empty collections.
Syntax
Queue<Type> queue = new Queue<Type>();\nStack<Type> stack = new Stack<Type>();Examples
Queue: First In, First Out
The first item added is the first one removed.
Queue<string> line = new Queue<string>();
line.Enqueue("Customer A");
line.Enqueue("Customer B");
line.Enqueue("Customer C");
Console.WriteLine(line.Dequeue()); // Customer A - first in, first out
Console.WriteLine(line.Peek()); // Customer B - look without removing
Console.WriteLine(line.Count); // 2- The first item added is the first one removed.
- Queue<T> is first-in, first-out: Enqueue adds at the back and Dequeue removes from the front. Stack<T> is last-in, first-out: Push adds at the top and Pop removes the top.
- Customer A is dequeued first. Peek then shows Customer B without removing it, and Count reports two people still waiting.
Stack: Last In, First Out
The most recently added item is the first one removed.
Stack<string> history = new Stack<string>();
history.Push("Page 1");
history.Push("Page 2");
history.Push("Page 3");
Console.WriteLine(history.Pop()); // Page 3 - last in, first out
Console.WriteLine(history.Peek()); // Page 2 - look without removing- The most recently added item is the first one removed.
- Queue<T> is first-in, first-out: Enqueue adds at the back and Dequeue removes from the front. Stack<T> is last-in, first-out: Push adds at the top and Pop removes the top.
- Page 3 was pushed last, so Pop returns it first. Peek shows Page 2 as the new top without removing it.
Practical Use: Balanced Parentheses Check
A classic use of Stack<T> for validating matched brackets.
bool IsBalanced(string input)
{
Stack<char> stack = new Stack<char>();
foreach (char c in input)
{
if (c == '(') stack.Push(c);
else if (c == ')')
{
if (stack.Count == 0) return false;
stack.Pop();
}
}
return stack.Count == 0;
}
Console.WriteLine(IsBalanced("(a(b)c)")); // true
Console.WriteLine(IsBalanced("(a(b)c")); // false- A classic use of Stack<T> for validating matched brackets.
- Queue<T> is first-in, first-out: Enqueue adds at the back and Dequeue removes from the front. Stack<T> is last-in, first-out: Push adds at the top and Pop removes the top.
- Every opening parenthesis is pushed; a closing one pops its match or fails if none exists. The first string returns True and the unfinished second returns False.
Best practices
- Use Queue<T> for FIFO processing like task scheduling or breadth-first traversal, and Stack<T> for LIFO needs like undo history or depth-first traversal
- Use Peek() to inspect the next item without removing it, and TryDequeue()/TryPop() to avoid an exception when the collection might be empty
- Prefer these purpose-built types over a List<T> with manual Insert(0, ...) or RemoveAt(list.Count - 1) - they are clearer in intent and more efficient
- Remember both are still generic collections (Queue<T>, Stack<T>) - specify the element type just like with List<T>
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference