Introducing List<T>, Stack<T>, Queue<T> and Dictionary<TKey, TValue> in C#

Explore powerful built-in data structures for real-world applications

C# and .NET provide a range of generic collections that go far beyond basic arrays. If you’re building anything more than a toy app, you’ll likely use List<T>, Stack<T>, Queue<T>, or Dictionary<TKey, TValue> at some point.

This article introduces each structure, when to use it, and how to use it effectively in your projects.


πŸ”Ή List<T> – Dynamic, Ordered Collection

The List<T> is the most versatile and commonly used collection in C#. It holds items of a specific type and keeps them in the order you add them.

List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Cherry");

foreach (var fruit in fruits)
{
    Console.WriteLine(fruit);
}

βœ… Best for:

  • Maintaining order
  • Frequently adding/removing items
  • Fast index-based access

πŸ”§ Key Methods:
Add(), Remove(), Insert(), Contains(), IndexOf(), Sort()


πŸ” Stack<T> – Last In, First Out (LIFO)

A Stack<T> works just like a stack of plates: the last thing you add is the first thing to come off. It’s perfect for undo systems, navigating history, or reversing data.

Stack<string> stack = new Stack<string>();
stack.Push("First");
stack.Push("Second");
stack.Push("Third");

Console.WriteLine(stack.Pop()); // Third
Console.WriteLine(stack.Peek()); // Second (still on top)

βœ… Best for:

  • Backtracking (undo, history)
  • Temporary storage where order matters
  • Reversing logic or operations

πŸ”§ Key Methods:
Push(), Pop(), Peek(), Count


🚦 Queue<T> – First In, First Out (FIFO)

A Queue<T> is like a line at the bank. The first item added is the first item retrieved.

Queue<string> queue = new Queue<string>();
queue.Enqueue("Alice");
queue.Enqueue("Bob");
queue.Enqueue("Charlie");

Console.WriteLine(queue.Dequeue()); // Alice
Console.WriteLine(queue.Peek());    // Bob (next in line)

βœ… Best for:

  • Processing requests in order
  • Scheduling tasks or events
  • Message buffers, print queues, pipelines

πŸ”§ Key Methods:
Enqueue(), Dequeue(), Peek(), Count


πŸ”‘ Dictionary<TKey, TValue> – Fast Key Lookup

A Dictionary<TKey, TValue> stores key-value pairs. It’s like a phonebook: you look up a key (like a name) and get a value (like a phone number).

Dictionary<string, int> ages = new Dictionary<string, int>();
ages["Alice"] = 30;
ages["Bob"] = 25;

Console.WriteLine(ages["Alice"]); // 30

βœ… Best for:

  • Mapping keys to values (like IDs to objects)
  • Fast lookups
  • Eliminating duplicate keys

πŸ”§ Key Methods:
Add(), ContainsKey(), Remove(), TryGetValue(), indexer []


🧠 Which to Use and When?

NeedUse ThisWhy
Ordered, index-based listList<T>Versatile, dynamic, widely supported
Undo or reverse logicStack<T>Last-in, first-out structure
Process items in arrival orderQueue<T>First-in, first-out behavior
Associate data by keyDictionary<K,V>Fast lookup using a key

πŸ” More Examples

πŸ”Ή Stack for Undo:

Stack<string> undoHistory = new Stack<string>();
undoHistory.Push("Edit 1");
undoHistory.Push("Edit 2");
Console.WriteLine("Undo: " + undoHistory.Pop());

πŸ”Ή Queue for Tasks:

Queue<string> taskQueue = new Queue<string>();
taskQueue.Enqueue("Load Image");
taskQueue.Enqueue("Resize Image");
taskQueue.Enqueue("Save Image");

while (taskQueue.Count > 0)
{
    Console.WriteLine("Processing: " + taskQueue.Dequeue());
}

πŸ”Ή Dictionary for ID Lookup:

Dictionary<int, string> products = new Dictionary<int, string>
{
    { 100, "Laptop" },
    { 200, "Tablet" },
    { 300, "Smartphone" }
};

if (products.ContainsKey(200))
    Console.WriteLine(products[200]); // Tablet

πŸ§ͺ Challenge Task

Write a simple app that:

  • Stores commands in a Stack<string> and lets the user undo them
  • Stores incoming usernames in a Queue<string> and processes them in order
  • Uses a Dictionary<string, int> to assign and look up user IDs

πŸ“š Summary

TypeDescriptionKey Feature
List<T>Ordered, resizable collectionAdd, index, LINQ
Stack<T>LIFO structurePush, Pop, Peek
Queue<T>FIFO structureEnqueue, Dequeue, Peek
Dictionary<K,V>Key/value mapFast key-based lookup

βœ… Best Practices

  • Use List<T> for most general-purpose collections
  • Use Stack<T> or Queue<T> when order of access matters
  • Use Dictionary<TKey, TValue> when you need fast lookups or unique keys
  • Always check .ContainsKey() or TryGetValue() before accessing dictionary values
  • Choose the right collection based on how you need to access your data

πŸ§‘β€πŸ’» Want more? Our hands-on .NET development training courses cover advanced data structures, LINQ, and custom collections β€” with real projects and live mentoring.