What is the difference between Array and ArrayList?

πŸ’‘ Concept: Array vs ArrayList

An Array is a fixed-size, strongly-typed collection of elements, while ArrayList is a dynamic, non-generic collection that can hold objects of any type.

πŸ“˜ Quick Intro

Arrays provide better performance and type safety. ArrayLists offer flexibility but require casting and have boxing/unboxing overhead.

🧠 Analogy

Think of an array as a row of lockers each with a specific key (type). An ArrayList is like a flexible storage box where anything can be thrown in but you have to check what’s inside when you take it out.

πŸ”§ Technical Explanation

  • 🧱 Arrays have a fixed size and strong type checking at compile time.
  • πŸ”„ ArrayLists are resizable and can hold objects of any type (non-generic).
  • ⚠️ ArrayLists require boxing/unboxing for value types, impacting performance.
  • 🎯 Arrays provide better performance and compile-time type safety.
  • πŸ”§ Use generic collections like List<T> instead of ArrayList in modern C#.

🎯 Use Cases

  • βœ… Use Arrays when the size is known and performance is critical.
  • βœ… Use ArrayLists when flexibility is needed but be cautious of type safety.
  • βœ… Prefer List<T> in most scenarios over ArrayList.
  • βœ… Useful for legacy code compatibility.

πŸ’» Code Example


// Array example
int[] numbers = new int[3];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;

// ArrayList example
ArrayList list = new ArrayList();
list.Add(1);
list.Add("Two");
list.Add(3.0);

❓ Interview Q&A

Q1: What is the main difference between Array and ArrayList?
A: Arrays are fixed size and type-safe; ArrayLists are dynamic and not type-safe.

Q2: Can you store different types in an ArrayList?
A: Yes, because it holds objects.

Q3: Is boxing involved with ArrayLists?
A: Yes, for value types.

Q4: Which is faster, Array or ArrayList?
A: Arrays are faster due to lack of boxing and type checks.

Q5: Should you use ArrayList in new code?
A: No, prefer List<T>.

Q6: Can you resize an Array?
A: No, arrays have fixed size.

Q7: How do you add items to an ArrayList?
A: Using Add() method.

Q8: Are Arrays zero-based?
A: Yes.

Q9: Can ArrayLists store value types?
A: Yes, but boxing occurs.

Q10: What namespace contains ArrayList?
A: System.Collections.

πŸ“ MCQs

Q1. Are Arrays fixed size?

  • Yes
  • No
  • Sometimes
  • Only in C#

Q2. Can ArrayLists hold multiple types?

  • No
  • Yes
  • Only primitives
  • Only objects

Q3. Do Arrays support boxing?

  • Yes
  • No
  • Only in .NET 5+
  • Sometimes

Q4. Which is faster, Array or ArrayList?

  • ArrayList
  • Array
  • Same
  • Depends on usage

Q5. Can Array size be changed after creation?

  • Yes
  • No
  • Only with Resize()
  • With dynamic arrays

Q6. Which collection is recommended over ArrayList?

  • Array
  • Hashtable
  • List<T>
  • Dictionary

Q7. How to add items to ArrayList?

  • Add()
  • Insert()
  • Append()
  • Push()

Q8. Are Arrays zero-based?

  • Yes
  • No
  • Depends
  • Sometimes

Q9. Does ArrayList involve boxing for value types?

  • No
  • Yes
  • Only for reference types
  • Never

Q10. Which namespace contains ArrayList?

  • System.Collections
  • System.Collections.Generic
  • System.Array
  • System.Core

πŸ’‘ Bonus Insight

While ArrayList provides flexibility for heterogeneous collections, modern C# favors generic collections like List<T> for better performance and type safety.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: