What is the difference between an array and a List<T> in C#?
Quick Answer
An array is a fixed-size, contiguous block of memory — fast, low overhead, but you can't add or remove elements. A List<T> wraps an array and grows automatically (doubling its internal capacity when full), so it supports Add/Remove at the cost of a little overhead and occasional reallocation.
An array is fixed size — you allocate it once and you can't grow it. A List<T> is backed by an array but manages resizing for you: when it fills up it allocates a bigger array, usually double, and copies. So arrays are leaner and I use them when the size is fixed or in performance-critical code, and List<T> is my default everywhere else because I usually don't know the count up front. If I do know it, I pass the capacity to the List constructor so it doesn't have to keep regrowing.
- Use an array when the size is known and fixed, in hot paths, or for interop; multidimensional data.
- Use a
List<T>for everything else — the default general-purpose collection. List<T>hasCount(elements) andCapacity(allocated slots); set the capacity in the constructor if you know the size to avoid regrowth.- Both are index-accessible in O(1); insert/remove in the middle of either is O(n).