C# > Advanced C# > Collections and Generics > Array vs List<T>
Array vs List<T>: A Practical Comparison
This snippet demonstrates the key differences between Arrays and List
Declaration and Initialization
Arrays are declared with a fixed size at the time of creation. List
// Array - Fixed Size
int[] numbersArray = new int[5];
numbersArray[0] = 1;
numbersArray[1] = 2;
// List<T> - Dynamic Size
List<int> numbersList = new List<int>();
numbersList.Add(1);
numbersList.Add(2);
Adding Elements
Adding elements to an array beyond its initial capacity requires creating a new array and copying the existing elements, which is an expensive operation. List
// Array - Need to create a new array to resize (inefficient)
int[] newNumbersArray = new int[numbersArray.Length + 1];
Array.Copy(numbersArray, newNumbersArray, numbersArray.Length);
newNumbersArray[numbersArray.Length] = 3;
numbersArray = newNumbersArray; // Re-assign to the updated array
// List<T> - Simple addition
numbersList.Add(3);
Insertion at a Specific Index
Inserting an element in the middle of an array requires shifting existing elements, leading to O(n) complexity where n is the number of elements after the index. List
// Array - Insertion requires shifting elements (inefficient)
int[] insertArray = new int[numbersArray.Length + 1];
Array.Copy(numbersArray, 0, insertArray, 0, 2);
insertArray[2] = 10;
Array.Copy(numbersArray, 2, insertArray, 3, numbersArray.Length - 2);
numbersArray = insertArray;
// List<T> - Inserting with shifting elements
numbersList.Insert(1, 10);
Removal of elements
Removing an element from an array also necessitates shifting elements, mirroring the insertion process in terms of complexity. List
// Array - Removal requires shifting elements (inefficient)
int removeIndex = 1;
int[] removeArray = new int[numbersArray.Length - 1];
Array.Copy(numbersArray, 0, removeArray, 0, removeIndex);
Array.Copy(numbersArray, removeIndex + 1, removeArray, removeIndex, numbersArray.Length - removeIndex - 1);
numbersArray = removeArray;
// List<T> - Removing elements
numbersList.Remove(10);
Performance: Array vs List
Memory Footprint
Arrays consume memory based on their declared size, regardless of how many elements are actually used. List
Real-Life Use Case
Best Practices
Interview Tip
Be prepared to discuss the performance trade-offs between arrays and List
When to Use Them
Pros and Cons
Pros: Fast element access, efficient memory usage when size is known.
Cons: Fixed size, inefficient for insertions and deletions.
Pros: Dynamic size, easy to add and remove elements.
Cons: Slower element access compared to arrays, potential memory overhead.
Alternatives
Depending on your needs, alternatives include:
FAQ
-
When should I use an array over a List
?
Use an array when you know the size of the collection beforehand, and you need fast access to elements by index. Arrays are also more memory-efficient when the size is known. -
What is the performance difference between accessing an element in an array versus a List
?
Accessing an element in an array by index is generally faster (O(1)) than accessing an element in a Listby index. However, the difference might be negligible in many scenarios. -
Is it possible to change the size of an array after it's created?
No, arrays in C# have a fixed size. To change the 'size', you would need to create a new array and copy the elements from the old array to the new array. Listdynamically manages its size.