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

Learn More About C# πŸ“š

1. What is C#? πŸ‘‰ Explained
2. Main Features of C# πŸ‘‰ Explained
3. Difference Between C# and Java πŸ‘‰ Explained
4. Common Language Runtime (CLR) in C# πŸ‘‰ Explained
5. Common Type System (CTS) in C# πŸ‘‰ Explained
6. Common Language Specification (CLS) in C# πŸ‘‰ Explained
7. Value Types vs Reference Types in C# πŸ‘‰ Explained
8. What is a Namespace in C#? πŸ‘‰ Explained
9. Purpose of the 'using' Keyword in C# πŸ‘‰ Explained
10. Different Data Types in C# πŸ‘‰ Explained
11. Difference Between int and Int32 in C# πŸ‘‰ Explained
12. Difference Between float, double, and decimal in C# πŸ‘‰ Explained
13. What is the Default Value of a Boolean in C#? πŸ‘‰ Explained
14. What is Boxing and Unboxing in C# πŸ‘‰ Explained
15. What are the Different Types of Operators in C# πŸ‘‰ Explained
16. Difference Between Equals and == in C# πŸ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# πŸ‘‰ Explained
18. What is the Ternary Operator in C# πŸ‘‰ Explained
19. How Does the Switch Statement Work in C# πŸ‘‰ Explained
20. What is Object-Oriented Programming in C# πŸ‘‰ Explained
21. What are the Four Pillars of OOP in C# πŸ‘‰ Explained
22. What is Encapsulation in C# πŸ‘‰ Explained
23. What is Inheritance in C# πŸ‘‰ Explained
24. What is Polymorphism in C# πŸ‘‰ Explained
25. What is Abstraction in C# πŸ‘‰ Explained
26. What is an Abstract Class in C# πŸ‘‰ Explained
27. What is an Interface in C# πŸ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? πŸ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# πŸ‘‰ Explained
30. How Do You Create a Class in C#? πŸ‘‰ Explained
31. What is a Constructor in C# πŸ‘‰ Explained
32. What Are the Types of Constructors in C# πŸ‘‰ Explained
33. What is a Static Constructor in C# πŸ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# πŸ‘‰ Explained
35. What is the Use of 'this' Keyword in C# πŸ‘‰ Explained
36. What is a Destructor in C# πŸ‘‰ Explained
37. What is Object Initializer Syntax in C# πŸ‘‰ Explained
38. What is the Difference Between Field and Property in C# πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup