Why collections over arrays in C Sharp (C#) - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how using collections instead of arrays affects the time it takes to do common tasks.
How does the choice between arrays and collections change the work done as data grows?
Analyze the time complexity of adding elements to an array versus a List<T> collection.
int[] numbers = new int[5];
for (int i = 0; i < 5; i++)
{
numbers[i] = i;
}
List<int> numberList = new List<int>();
for (int i = 0; i < 5; i++)
{
numberList.Add(i);
}
This code fills a fixed-size array and a dynamic list with 5 numbers each.
Look at the loops and operations that repeat as we add items.
- Primary operation: Assigning or adding an element inside a loop.
- How many times: Exactly 5 times for both array and list in this example.
As we add more items, the array assignment stays simple but the list may need extra work.
| Input Size (n) | Approx. Operations for Array | Approx. Operations for List |
|---|---|---|
| 10 | 10 assignments | About 10 adds, some resizing |
| 100 | 100 assignments | About 100 adds, several resizes |
| 1000 | 1000 assignments | About 1000 adds, multiple resizes |
Array operations grow directly with input size. List operations also grow but resizing causes extra work sometimes.
Time Complexity: O(n)
This means both array and list take time proportional to the number of items added, but lists handle resizing behind the scenes.
[X] Wrong: "Adding to a List is always slower than arrays because of resizing overhead."
[OK] Correct: Lists resize only occasionally, so most adds are fast. Over many adds, the average time per add stays low.
Understanding how collections manage time costs helps you explain why they are often preferred over arrays for flexible data. This skill shows you think about efficiency and practical coding.
What if we changed the List to a LinkedList? How would the time complexity of adding elements change?
Practice
List<T> over an array in C#?Solution
Step 1: Understand array size behavior
Arrays in C# have a fixed size once created and cannot grow or shrink.Step 2: Understand collection size behavior
Collections likeList<T>can dynamically add or remove items, changing their size.Final Answer:
Because collections can change size dynamically while arrays have fixed size. -> Option AQuick Check:
Collections grow/shrink; arrays fixed size [OK]
- Thinking arrays can resize automatically
- Believing collections use less memory always
- Confusing data type storage capabilities
Solution
Step 1: Check syntax for array declaration
int[] numbers = new int[5];declares a fixed-size array, not a collection.Step 2: Check syntax for collection declaration
List<int> numbers = new List<int>();correctly declares a generic list collection that can grow.Final Answer:
List<int> numbers = new List<int>(); -> Option BQuick Check:
List<T> syntax is for growable collections [OK]
- Using array syntax when collection is needed
- Assigning wrong types to variables
- Confusing ArrayList with arrays
List<int> nums = new List<int>() {1, 2, 3};
nums.Add(4);
Console.WriteLine(nums.Count);Solution
Step 1: Understand initial list size
The listnumsstarts with 3 elements: 1, 2, 3.Step 2: Analyze the Add method effect
Callingnums.Add(4);adds one more element, increasing count to 4.Final Answer:
4 -> Option DQuick Check:
List.Count reflects added elements [OK]
- Assuming Count stays 3 after Add
- Confusing Count with capacity
- Expecting compilation error due to Add
int[] arr = new int[3]; arr.Add(5);
Solution
Step 1: Check array methods
Arrays in C# do not have anAddmethod; this method belongs to collections like List.Step 2: Understand array limitations
Arrays have fixed size and cannot add elements dynamically.Final Answer:
Arrays do not have an Add method. -> Option CQuick Check:
Arrays lack Add method [OK]
- Trying to add elements to arrays
- Confusing array size with element count
- Assuming arrays have collection methods
Solution
Step 1: Analyze data size flexibility needs
User names list changes size, so fixed size arrays are inconvenient.Step 2: Choose collection type for dynamic data
List<string>allows adding/removing names easily without recreating the structure.Final Answer:
Use a List<string> because it can grow and shrink as users are added or removed. -> Option AQuick Check:
Dynamic data needs collections like List [OK]
- Choosing fixed arrays for changing data
- Using strings to store multiple values unsafely
- Recreating arrays repeatedly instead of collections
