C# Collections: List, Dictionary, HashSet and Queue

Collections in C# provide convenient ways to store and manage groups of objects. Different collection types are designed for different tasks, such as ordered data, key-value lookups, unique values, and queue-based processing.

What are Collections in C#?

A collection is an object that stores multiple values. The .NET platform provides several generic collection classes that offer type safety and useful operations for managing data.

CSHARP
List<string> names = new List<string>
{
    "Alice",
    "Bob",
    "Charlie"
};

Why Use Collections?

  • Store multiple values efficiently
  • Add and remove items dynamically
  • Search and sort data
  • Access values using keys
  • Build reusable data structures

Using List

List is one of the most commonly used collections in C#. It stores elements in an ordered sequence and allows duplicate values.

CSHARP
List<int> numbers = new List<int>();

numbers.Add(10);
numbers.Add(20);
numbers.Add(30);

Console.WriteLine(numbers[0]);

Common List Operations

CSHARP
numbers.Add(40);
numbers.Remove(20);
numbers.Contains(30);
numbers.Sort();

Console.WriteLine(numbers.Count);

Using Dictionary

Dictionary stores data as key-value pairs. It is useful when you need to quickly find a value using a unique key.

CSHARP
Dictionary<int, string> users = new Dictionary<int, string>();

users[1] = "Alice";
users[2] = "Bob";

Console.WriteLine(users[1]);

Using HashSet

HashSet stores unique values. If the same value is added more than once, the collection keeps only one instance.

CSHARP
HashSet<string> languages = new HashSet<string>();

languages.Add("C#");
languages.Add("Java");
languages.Add("C#");

Console.WriteLine(languages.Count);

Using Queue

Queue follows the FIFO principle, meaning the first item added is the first item removed.

CSHARP
Queue<string> tasks = new Queue<string>();

tasks.Enqueue("Task 1");
tasks.Enqueue("Task 2");

tasks.Enqueue("Task 3");

string nextTask = tasks.Dequeue();

Using Stack

Stack follows the LIFO principle, meaning the last item added is the first item removed.

CSHARP
Stack<string> pages = new Stack<string>();

pages.Push("Home");
pages.Push("Products");
pages.Push("Details");

string previousPage = pages.Pop();

C# Collection Comparison

CollectionMain PurposeAllows DuplicatesAccess Style
ListOrdered collectionYesIndex
DictionaryKey-value lookupKeys must be uniqueKey
HashSetUnique valuesNoValue
QueueFIFO processingYesFront
StackLIFO processingYesTop

When Should You Use List?

  • When item order matters
  • When you need index-based access
  • When duplicate values are allowed
  • When the collection size changes dynamically

When Should You Use Dictionary?

  • When data has unique keys
  • When fast key-based lookup is needed
  • When representing key-value relationships
  • When accessing records by an identifier

When Should You Use HashSet?

  • When duplicate values should be prevented
  • When membership checks are common
  • When working with unique identifiers
  • When performing set operations

Real-World Example

CSHARP
Dictionary<int, string> products = new Dictionary<int, string>
{
    { 101, "Laptop" },
    { 102, "Keyboard" },
    { 103, "Mouse" }
};

foreach (var product in products)
{
    Console.WriteLine($"{product.Key}: {product.Value}");
}

Common Mistakes to Avoid

  • Using List when key-based lookup is required
  • Expecting HashSet to preserve list-style indexing
  • Accessing a missing Dictionary key without checking
  • Using Stack when FIFO processing is required
  • Using Queue when LIFO behavior is needed

Advanced Collection Concepts

  • IEnumerable
  • ICollection
  • IList
  • SortedDictionary
  • SortedSet
  • Concurrent collections
  • Immutable collections

Practice Exercises

  • Create a list of student names
  • Build a dictionary of product IDs and names
  • Remove duplicate values using HashSet
  • Create a queue for customer requests
  • Build a stack for browser history
  • Compare two collections using set operations

Conclusion

Choosing the right collection can make C# applications easier to design and maintain. List, Dictionary, HashSet, Queue, and Stack each solve different data-management problems, so understanding their behavior helps developers select the right tool for each situation.

Note: Note: Select a collection based on how your application needs to store, access, search, and process its data.