LearnThatStack Ace your next interview
C# · question
Question 13 of 112

Explain the difference between Array, ArrayList, and List<T>.

beginner
← All C# questions
Re-explain
Visual ↓

An array has a fixed size, while ArrayList and List<T> grow as you add items. Arrays and List<T> are type-safe, and ArrayList is not.

ArrayList stores everything as object. Adding an int boxes it, and nothing stops you mixing strings and numbers in one list. The mistake shows up at run time as a failed cast. List<T> is generic, so the compiler checks every Add, and at run time it stores value types without boxing.

Array ArrayList List<T>
Size Fixed Grows Grows
Type-safe Yes No Yes
Boxes value types No Yes No
Use it for Fixed-size data, best performance Legacy code only Most collections

All three use zero-based indexing. Prefer List<T> in new code, and an array when the size is known and fixed.

// Array
int[] array = new int[5];
// ArrayList (avoid)
ArrayList arrayList = new ArrayList();
arrayList.Add(1);       // Boxing occurs
arrayList.Add("text");  // No compile-time type checking
// List<T> (preferred)
List<int> list = new List<int>();
list.Add(1);           // No boxing
// list.Add("text");   // Compile-time error
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
Where ArrayList and List<int> keep their items
after arrayList.Add(1)arrayList.Add(1);
Variables
arrayList1
Heap
1ArrayList (object[])
[0]2
2Int32 (boxed)
value1

Each slot holds an object reference, so C# boxes the 1. That copies it into a new heap object.

after Add("text")arrayList.Add("text");
Variables
arrayList1
Heap
1ArrayList (object[])
[0]2
[1]new3
2Int32 (boxed)
value1
3Stringnew
value"text"

Nothing stops a string. (int)arrayList[1] compiles, then throws InvalidCastException at run time.

List<int> after Add(1)list.Add(1);
Variables
listnew4
Heap
4List<int> (int[])new
[0]1

List<int> keeps the 1 inside its own int[], with no box. The compiler rejects list.Add("text").

Every ArrayList slot points to a separate heap object of any type. List<int> keeps the ints themselves inside an int[], with no boxing.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • What happens inside a List<T> when you add an item and it is already at capacity? It allocates a larger backing array, roughly double, and copies the items across. Passing the expected count to the constructor avoids repeated copies.
  • Why does boxing an int into an ArrayList cost you, and what happens when you read it back? Each boxed int is a separate heap allocation for the GC to collect, and reading it back needs an unboxing cast that throws on the wrong type.
  • If List<T> is not the right fit, which other generic collections would you reach for? Match each to an access pattern: Dictionary for lookup by key, HashSet for uniqueness and fast Contains, Queue and Stack for order.
  • Should a public method return List<T>, or something else? Return the narrowest type the caller needs, like IReadOnlyList<T> or IEnumerable<T>, so callers can't change your internal list.
  • Arrays in C# are covariant. What can go wrong that List<T> prevents? Assign a string[] to object[], store an int, and it throws ArrayTypeMismatchException at run time. List<T> is invariant, so that won't compile.

What you can say

  1. An array has a fixed size, while ArrayList and List<T> both grow as you add items.
  2. Arrays and List<T> are type-safe, and ArrayList isn't, because it stores everything as object.
  3. So adding an int to an ArrayList boxes it, and nothing stops you mixing strings and numbers, so the bug only shows up at run time as a failed cast.
  4. List<T> is generic, so the compiler checks every Add, and at run time it stores value types like int without boxing.
  5. All three use zero-based indexing, so day-to-day access looks the same.
  6. In new code I default to List<T>, use an array when the size is known and fixed, and leave ArrayList to legacy code.

Weak answers to avoid

  • Treats ArrayList and List<T> as the same thing They differ where it matters. ArrayList stores object, so ints get boxed and type errors surface at run time. List<T> is checked at compile time.
  • Calls List<T> a linked list List<T> is backed by an array that it resizes, so indexing is as fast as an array. Calling it a linked list gets indexing and insert costs wrong.
  • Recites the table without the reason Saying ArrayList isn't type-safe is half an answer. Explain that it stores object, so a bad cast fails at run time instead of at compile time.
  • Suggests ArrayList when items have mixed types Mixing types is the bug ArrayList fails to catch. If the items share behavior, use a List<T> of their common base class or interface.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C# cheatsheet.

← Back to all C# questions
Pro · $10/mo

98 of 112 C# answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime