C# and .NET • Modern C# baseline
Arrays, Lists, and Collection Choices
Choose collections based on required operations and ownership.
Arrays have a fixed length. List<T> grows as items are added and is a common default for ordered mutable data. Dictionary<TKey,TValue> supports key-based lookup, while HashSet<T> tracks unique membership. Collection choice influences performance and the guarantees callers can rely on.
Key points
- Use generic collections to preserve element types.
- Do not expose a mutable collection when callers should not change internal state.
Example
using System.Collections.Generic;
var names = new List<string> { "Ada", "Lin" };
names.Add("Sam");
var uniqueNames = new HashSet<string>(names);
Console.WriteLine($"{names.Count} entries, {uniqueNames.Count} unique");
What to expect
3 entries, 3 unique
Practice exercise
Choose a collection for counting word frequencies and justify the lookup behavior you need.
Key idea: Select a collection from the operations and invariants your code requires.