1. 🔴 What is the difference between List<T> and ArrayList?
What is it?
-
List<T>is a generic collection that stores elements of a specified type. -
ArrayListis a non-generic collection that stores elements asobject.
Comparison
|
|
|
|---|---|
|
Generic collection |
Non-generic collection |
|
Type-safe |
Not type-safe |
|
Uses |
Stores |
|
No boxing for value types |
Boxing/unboxing for value types |
|
No casting required |
Casting often required |
|
Recommended for modern C# |
Mainly used in legacy code |
Example
// ArrayList
ArrayList items = new ArrayList();
items.Add(10);
items.Add("Hello");
int number = (int)items[0]; // Casting required
// List<T>
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
// numbers.Add("Hello"); // Compile-time error
int value = numbers[0]; // No casting
Explanation
-
ArrayListallows different data types. -
List<int>allows only integers. -
ArrayListrequires boxing when storing integers. -
List<int>avoids boxing for stored integers.
Key Points
-
List<T>provides better type safety. -
List<T>generally performs better for value types. -
Both collections dynamically resize.
-
Interview: Prefer
List<T>overArrayListin modern applications.
2. 🔴 What is the difference between an array and List<T>?
What is it?
An array stores a fixed number of elements, whereas List<T> is a dynamically resizable collection.
Comparison
|
Array |
|
|---|---|
|
Fixed size |
Dynamic size |
|
|
|
|
Uses |
Uses |
|
Cannot directly add elements |
Supports |
|
Lower memory overhead |
Additional capacity overhead |
|
Suitable for fixed-size data |
Suitable for changing data |
Example
// Array
int[] numbers = { 10, 20, 30 };
Console.WriteLine(numbers.Length); // 3
// List<T>
List<int> values = new List<int>();
values.Add(10);
values.Add(20);
values.Add(30);
values.Add(40);
Console.WriteLine(values.Count); // 4
Explanation
-
Array size cannot change after creation.
-
List<T>automatically expands when required. -
Both support index-based access.
Diagram
Array
+----+----+----+
| 10 | 20 | 30 |
+----+----+----+
Size = 3 (Fixed)
List<int>
+----+----+----+
| 10 | 20 | 30 |
+----+----+----+
|
| Add(40)
v
+----+----+----+----+
| 10 | 20 | 30 | 40 |
+----+----+----+----+
Count = 4 (Dynamic)
Key Points
-
Arrays are useful when the number of elements is fixed.
-
List<T>is useful when elements are frequently added or removed. -
Both support O(1) indexed access.
-
Internally,
List<T>uses an array.
3. 🔴 What is the difference between Count and Capacity in List<T>?
What is it?
-
Count: Number of elements currently stored in the list.
-
Capacity: Number of elements the internal array can hold before resizing.
Example
List<int> numbers = new List<int>(5);
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
Console.WriteLine(numbers.Count); // 3
Console.WriteLine(numbers.Capacity); // 5
Explanation
-
new List<int>(5)allocates space for 5 integers. -
Only 3 elements are added.
-
Therefore,
Count = 3andCapacity = 5.
Diagram
List<int> Capacity = 5
Index: 0 1 2 3 4
+----+----+----+----+----+
| 10 | 20 | 30 | | |
+----+----+----+----+----+
Used Elements Free Slots
Count = 3
Capacity = 5
Comparison
|
Count |
Capacity |
|---|---|
|
Actual number of elements |
Allocated storage size |
|
Increases when adding elements |
Increases when resizing is needed |
|
Decreases when removing elements |
Does not automatically decrease on removal |
|
Cannot be directly assigned |
Can be configured |
Key Points
-
Countis always less than or equal toCapacity. -
Capacitycan be larger thanCount. -
Setting an initial capacity can reduce memory reallocations.
-
Clear()resetsCountto zero but normally retainsCapacity.
4. 🟡 What happens internally when a List<T> exceeds its capacity?
What happens?
When a List<T> exceeds its capacity, .NET automatically allocates a larger internal array and copies existing elements into it.
Internal Process
Step 1: Existing List
Capacity = 4
Count = 4
+----+----+----+----+
| 10 | 20 | 30 | 40 |
+----+----+----+----+
Step 2: Add(50)
Current array is full.
New capacity is required.
Step 3: Allocate New Array
Capacity = 8
+----+----+----+----+----+----+----+----+
| | | | | | | | |
+----+----+----+----+----+----+----+----+
Step 4: Copy Existing Elements and Add 50
+----+----+----+----+----+----+----+----+
| 10 | 20 | 30 | 40 | 50 | | | |
+----+----+----+----+----+----+----+----+
Count = 5
Capacity = 8
Example
List<int> numbers = new List<int>(2);
numbers.Add(10);
numbers.Add(20);
Console.WriteLine(numbers.Capacity); // 2
numbers.Add(30);
Console.WriteLine(numbers.Capacity); // 4
Console.WriteLine(numbers.Count); // 3
Explanation
-
Initially, the list has a capacity of 2.
-
Two elements are added.
-
Adding the third element exceeds the capacity.
-
.NET allocates a new internal array with capacity 4.
-
Existing elements are copied, and the new element is inserted.
-
The old array becomes eligible for garbage collection when no references remain.
Key Points
-
List<T>internally maintains an array. -
Capacity typically doubles during growth, although this is an implementation detail.
-
Resizing requires O(n) time to copy existing elements.
-
Normal
Add()operations have amortized O(1) complexity. -
Use
new List<int>(1000)when the expected size is known to reduce resizing.
Quick Interview Revision
|
Question |
Short Answer |
|---|---|
|
|
Generic/type-safe vs non-generic/object-based |
|
Array vs |
Fixed size vs dynamic size |
|
Count vs Capacity |
Actual elements vs allocated storage |
|
What happens when capacity is exceeded? |
A larger array is allocated and existing elements are copied |
Important Interview Point: List<T> is not a linked list. It is implemented using a dynamically resizable array.