List vs Array in C#: Key Differences and When to Use Each
List<T> is a flexible, resizable collection that allows easy addition and removal of items, while an Array has a fixed size and better performance for fixed-size data. Use List<T> when you need dynamic sizing and Array when you know the size in advance and want faster access.Quick Comparison
Here is a quick side-by-side comparison of List<T> and Array in C# based on key factors.
| Factor | List<T> | Array |
|---|---|---|
| Size | Dynamic, can grow or shrink | Fixed, size set at creation |
| Performance | Slightly slower due to resizing and overhead | Faster for fixed size and direct access |
| Flexibility | Supports adding/removing elements easily | No adding/removing after creation |
| Memory | Uses more memory due to overhead | More memory efficient |
| Usage | Best for collections with unknown or changing size | Best for fixed-size collections |
| Type Safety | Generic and type-safe | Type-safe but fixed type |
Key Differences
Array in C# is a fixed-size collection that you must define with a specific length when you create it. Once created, you cannot change its size. This makes arrays very fast and memory efficient because the system knows exactly how much space to allocate. Arrays are great when you know the exact number of elements beforehand.
On the other hand, List<T> is a generic collection that can grow or shrink dynamically. It manages an internal array and resizes it automatically when you add or remove items. This flexibility comes with a small performance cost and slightly more memory usage. Lists provide many useful methods like Add, Remove, and Insert that make managing collections easier.
In summary, use Array for fixed-size, performance-critical scenarios, and use List<T> when you need a flexible, easy-to-use collection that can change size during runtime.
Code Comparison
This example shows how to create a collection of numbers, add an item, and print all items using an Array in C#.
using System; class Program { static void Main() { int[] numbers = new int[3] { 1, 2, 3 }; // Arrays have fixed size, so we cannot add directly // To add, we create a new array with larger size int[] newNumbers = new int[numbers.Length + 1]; for (int i = 0; i < numbers.Length; i++) { newNumbers[i] = numbers[i]; } newNumbers[newNumbers.Length - 1] = 4; numbers = newNumbers; foreach (int num in numbers) { Console.Write(num + " "); } } }
List<T> Equivalent
This example shows the same task using List<int>, which is simpler and more flexible.
using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> { 1, 2, 3 }; numbers.Add(4); // Easy to add new item foreach (int num in numbers) { Console.Write(num + " "); } } }
When to Use Which
Choose Array when you know the exact number of elements in advance and want the best performance with minimal memory overhead. Arrays are ideal for fixed-size data like storing coordinates or fixed-length buffers.
Choose List<T> when you need a collection that can grow or shrink dynamically, or when you want easy methods to add, remove, or search items. Lists are perfect for most general-purpose programming where collection size changes over time.
Key Takeaways
Array for fixed-size, performance-critical collections.List<T> for flexible, resizable collections with easy management.