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.
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
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
Console.WriteLine(numbers[0]);
Common List Operations
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.
Dictionary<int, string> users = new Dictionary<int, string>();
users[1] = "Alice";
users[2] = "Bob";
Console.WriteLine(users[1]);
Using HashSet
HashSet
HashSet<string> languages = new HashSet<string>();
languages.Add("C#");
languages.Add("Java");
languages.Add("C#");
Console.WriteLine(languages.Count);
Using Queue
Queue
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
Stack<string> pages = new Stack<string>();
pages.Push("Home");
pages.Push("Products");
pages.Push("Details");
string previousPage = pages.Pop();
C# Collection Comparison
| Collection | Main Purpose | Allows Duplicates | Access Style |
|---|---|---|---|
| List | Ordered collection | Yes | Index |
| Dictionary | Key-value lookup | Keys must be unique | Key |
| HashSet | Unique values | No | Value |
| Queue | FIFO processing | Yes | Front |
| Stack | LIFO processing | Yes | Top |
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
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.