Array vs List in C#: which should I use?
Short answer
Use List<T> by default — it grows, and it has the methods you want. Use an array when the size is genuinely fixed and known, or when an API demands one.
int[] fixedScores = new int[3]; // fixed length, forever
int[] literal = { 88, 92, 79 };
var scores = new List<int> { 88, 92, 79 };
scores.Add(95);
scores.Remove(79);#What List gives you
scores.Add(x); scores.AddRange(other);
scores.Insert(0, x);
scores.Remove(x); scores.RemoveAt(0);
scores.Contains(x); scores.IndexOf(x);
scores.Sort(); scores.Reverse();
scores.Count;Note Count on a List and Length on an array — a small inconsistency that trips people up constantly.
#How List grows
Internally it is an array. When it fills up, it allocates a new one at double the capacity and copies. If you know roughly how many items you will add, pre-size it:
var scores = new List<int>(1000);#Other collections worth knowing
| Need | Type |
|---|---|
| Key to value | Dictionary<K,V> |
| Unique items, fast lookup | HashSet<T> |
| First in, first out | Queue<T> |
| Last in, first out | Stack<T> |
| Read-only view | IReadOnlyList<T> |
| Thread-safe | ConcurrentDictionary<K,V> |
#Accept the interface, return the concrete type
void Process(IEnumerable<int> items) { } // accepts arrays, lists, LINQ results
List<int> Build() => new();Accepting IEnumerable<T> makes your method usable with any sequence.