/
đ C# Collections Explained: List, Dictionary, HashSet and When to Use Each
# C# Collections Explained: List, Dictionary, HashSet and When to Use Each
Choosing the right collection can make C# code simpler and faster. The most common choices are `List`, `Dictionary`, and `HashSet`.
## List
Use `List` when you need an ordered sequence and frequently access items by position.
```csharp
var names = new List { "John", "Mary", "Alex" };
Console.WriteLine(names[0]);
```
## Dictionary
Use a dictionary when you need to find a value by a key.
```csharp
var employees = new Dictionary
{
[101] = "John",
[102] = "Mary"
};
Console.WriteLine(employees[101]);
```
## HashSet
Use a hash set when uniqueness matters and you mainly need fast membership checks.
```csharp
var codes = new HashSet { "A", "B", "A" };
Console.WriteLine(codes.Count); // 2
```
## Which should you choose?
Choose `List` for ordered data, `Dictionary` for key-based lookup, and `HashSet` for unique values and membership tests.
## Summary
The best collection is the one that matches the operation your application performs most often. Avoid choosing a collection only because it is familiar.
Comments will appear here when available.