Queue and Stack Collections in C#

Vaibhav • September 10, 2025

In earlier lessons, we explored arrays and various collection types like lists and dictionaries. Now it’s time to study two fundamental data structures that are widely used in programming - Queue and Stack. Both are part of the System.Collections.Generic namespace and are designed to store elements in a particular order while enforcing specific rules for insertion and removal.

In this article, we’ll explore how queues and stacks work, their real-world analogies, methods and properties, performance considerations, and best practices. These structures are foundational in computer science and understanding them will make you a stronger problem-solver.

Understanding Queues

A Queue is a collection that follows the FIFO (First-In, First-Out) principle. The first element added to the queue will be the first one removed. Think of it like a line at a ticket counter - the first person in line is served first.

Queues are useful when you need to process items in the order they arrive, such as background tasks, print jobs, or message passing.

Declaring and Initializing a Queue


// Declaring a queue of integers
Queue numbers = new Queue();

// Adding items (Enqueue)
numbers.Enqueue(10);
numbers.Enqueue(20);
numbers.Enqueue(30);
    

At this point, the queue contains 10 → 20 → 30 in that order.

Removing and Accessing Elements


// Removing the first element (Dequeue)
int first = numbers.Dequeue();
Console.WriteLine(first); // Output: 10

// Peeking at the next item without removing
int next = numbers.Peek();
Console.WriteLine(next); // Output: 20
    

Dequeue() removes and returns the oldest element, while Peek() lets you look at the next element without altering the queue.

Queue Properties and Methods

  • Count - tells how many items are in the queue.
  • Clear() - removes all items.
  • Contains(item) - checks if a specific item exists.
  • Queues can be enumerated with foreach to process all items in order.

// Example: Iterating through a queue
foreach (int n in numbers)
{
    Console.WriteLine(n);
}
    

Understanding Stacks

A Stack is a collection that follows the LIFO (Last-In, First-Out) principle. The last item added is the first one removed. A common analogy is a stack of plates - the last plate placed on top is the first one you take off.

Stacks are essential in areas like method calls (the call stack), undo/redo functionality, and expression evaluation.

Declaring and Initializing a Stack


// Declaring a stack of strings
Stack books = new Stack();

// Adding items (Push)
books.Push("C# Basics");
books.Push("OOP Concepts");
books.Push("Data Structures");
    

At this point, the stack looks like [Top → Data Structures, OOP Concepts, C# Basics].

Removing and Accessing Elements


// Removing the top item (Pop)
string topBook = books.Pop();
Console.WriteLine(topBook); // Output: Data Structures

// Peeking at the top without removing
string peekBook = books.Peek();
Console.WriteLine(peekBook); // Output: OOP Concepts
    

Stack Properties and Methods

  • Count - returns the number of elements in the stack.
  • Clear() - empties the stack.
  • Contains(item) - checks if the stack contains a specific item.
  • Stacks can also be enumerated using foreach, starting from the top element.

Performance Considerations

Both Queue<T> and Stack<T> are highly efficient for their intended operations:

  • Enqueue() and Dequeue() run in amortized O(1) time.
  • Push() and Pop() also run in O(1) time.
  • Peek() is constant time.
  • Iteration through the entire collection is O(n).

These characteristics make queues and stacks extremely fast when used appropriately, though they are not designed for random access like arrays or lists.

When to Use Queues vs Stacks

  • Use a Queue when you need to process items in the order they arrive (FIFO).
  • Use a Stack when you need to process items in reverse order (LIFO).
Trying to use both queue and stack interchangeably often leads to confusion. Always choose the right one based on your access pattern.

Summary

Queues and stacks are specialized collections with simple but powerful rules. The queue ensures FIFO ordering, while the stack enforces LIFO ordering. Both are efficient, reliable, and form the backbone of many algorithms and system operations. By mastering these, you’ll gain a deeper appreciation of how data flows in real-world programming scenarios.