Choosing a Collection
Trade-offs and performance.
One Question First
Choosing a collection starts with one question: how will you access the data? By position, by key, or just check membership?
List, Dictionary, and HashSet each answer a different access pattern. Match the tool to the pattern and your code stays fast and clear.
Access by Position: List
If order matters and you reach items by index, choose List<T>. It keeps insertion order and gives O(1) indexing.
Examples: a queue of steps, rows in display order, or any sequence you iterate front to back. Duplicates are allowed.
var steps = new List<string> { "mix", "bake", "cool" };
string first = steps[0]; // O(1) by indexAll lessons in this course
- List in Practice
- Dictionary Lookups
- HashSet and Uniqueness
- Choosing a Collection