Short answer: use a List<T> by default, and an array when the size never changes.
But here's what most tutorials leave out: a List<T> is an array underneath.
Watch a List grow
List<int> numbers = new List<int>();
Console.WriteLine($"Count: {numbers.Count}, Capacity: {numbers.Capacity}");
for (int i = 1; i <= 9; i++)
{
numbers.Add(i);
Console.WriteLine($"Count: {numbers.Count}, Capacity: {numbers.Capacity}");
}
Capacity goes 0 → 4 → 8 → 16. When the internal array is full, the List creates a new array twice as big, copies every item, and throws the old one away.
If you know the size up front, skip the copying:
List<int> bigList = new List<int>(1000);
When an array is the better choice
When the size is fixed: days of the week, RGB values, a tic-tac-toe board. Arrays have no spare capacity and no resizing, but for beginner apps the difference is tiny.
Two common List mistakes
1. Reverse() doesn't sort. It only flips the order. Call Sort() first, then Reverse().
2. Removing items inside foreach crashes with "Collection was modified." Use RemoveAll instead:
nums.RemoveAll(n => n % 2 == 0);
Full video (5 min)
Free code: https://github.com/NextStepCSharp/nextstepcsharp-videos/tree/main/01-list-vs-array
Top comments (0)