Array vs ArrayList in .NET: Side-by-Side Guide

💡 Concept Name

Array: Fixed-length, strictly typed, and lightning-fast for data of a single kind.
ArrayList: Grows as needed, can hold any object type, but sacrifices safety and performance.

📘 Quick Intro

Deciding between Array and ArrayList in .NET? Arrays keep things neat and type-checked, but you need to know the exact size ahead of time. ArrayList gives you flexibility for unpredictable lists, but can lead to bugs if you’re not careful. Let’s dig in with real-world comparisons and code!

🧠 Analogy / Short Story

Picture an Array as a row of numbered, labeled lockers—each only accepts one kind of item, and you always know how many lockers there are. Now picture an ArrayList as a growing storage bin: toss in books, shoes, or snacks—anything goes, but you’ll have to dig around and double-check what’s inside when you want something specific.

🔧 Technical Explanation

  • 🔢 Arrays: You pick the type and the length at creation. Once set, both stay locked in. No mixing types or resizing later.
  • 📦 ArrayList: Add, remove, or mix items of any type at any time. But every item is stored as an object, so you lose type safety and might need to cast.
  • 🚀 Speed: Arrays run faster, since the compiler knows what to expect. ArrayList can slow things down, especially with value types or big loops.
  • 🧠 Modern .NET: Prefer List<T> for type safety and flexibility unless you really need old-school compatibility.

Fun fact: If you accidentally add a string to an ArrayList meant for numbers, you’ll only know at runtime—not during compile time, which can mean nasty bugs in production!

🎯 Purpose & Use Case

  • ✅ Use Array for: Known-length collections, numeric data, or whenever type safety and speed matter.
  • ✅ Use ArrayList for: Old codebases, mixed data, or when you don’t know the final list length and don’t need strict typing (rare today).
  • ✅ In modern C#, favor List<T> for both flexibility and type safety.

💻 Real Code Example

// Array Example (strictly typed)
int[] numbers = new int[] { 10, 20, 30 };
Console.WriteLine(numbers[1]); // Output: 20

// ArrayList Example (accepts any type)
ArrayList things = new ArrayList();
things.Add(5);
things.Add("hello");
things.Add(DateTime.Now);
Console.WriteLine(things[1]); // Output: hello

// Note: Using List is the modern approach:
List fruits = new List { "Apple", "Banana" };

❓ Interview Q&A

Q1: How does ArrayList handle different data types?
A: ArrayList can hold any object type, but you lose compile-time type checking and may need to cast.

Q2: Why are arrays faster than ArrayLists?
A: Arrays are type-safe and the compiler knows exactly how to store and access their values, so no runtime type checking or boxing/unboxing is needed.

Q3: When should you avoid ArrayList?
A: In new .NET code—prefer List<T> for safety, speed, and clarity.

Q4: What happens if you exceed an array’s bounds?
A: You’ll get an IndexOutOfRangeException at runtime.

Q5: Can arrays in .NET be resized after creation?
A: No, their size is fixed.

Q6: What’s the default namespace for ArrayList?
A: System.Collections.

Q7: Can ArrayList hold null values?
A: Yes, since it stores references to objects.

Q8: What is boxing/unboxing in the context of ArrayList?
A: Converting value types to objects (boxing) and back again (unboxing), which can hurt performance.

Q9: Can you create multi-dimensional arrays?
A: Yes, arrays can be one-, two-, or even multi-dimensional.

Q10: Which is more memory-efficient: Array or ArrayList?
A: Array, because there’s no overhead from storing as object or frequent resizing.

📝 MCQs

Q1. What’s a major benefit of using Array in .NET?

  • Type safety and performance
  • Holds mixed types
  • Dynamic resizing
  • Stores only strings

Q2. Why can ArrayList slow down performance?

  • Boxing/unboxing value types
  • Uses threads
  • Is compiled at runtime
  • Stores strings only

Q3. Which is the modern replacement for ArrayList?

  • Dictionary
  • Stack
  • List<T>
  • Queue

Q4. What happens if you add a string to an Array of integers?

  • Compile-time error
  • Runtime exception
  • No effect
  • Casts automatically

Q5. Which namespace contains ArrayList?

  • System.Collections
  • System.Lists
  • System.Data
  • System.Linq

Q6. How do you add items to an ArrayList?

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

Q7. Is ArrayList generic?

  • Yes
  • No
  • Configurable
  • Only for string

Q8. Which collection is best for fixed-size, high-speed data?

  • Array
  • ArrayList
  • Queue
  • HashSet

Q9. Can ArrayList store value and reference types?

  • Yes, but value types are boxed
  • No, only reference types
  • No, only value types
  • Yes, no boxing needed

Q10. Which should you use for new C# projects?

  • ArrayList
  • Array
  • List<T>
  • Dictionary

💡 Bonus Insight

Today’s best practice: use List<T> for anything dynamic in .NET. You’ll get the speed and predictability of arrays, but with the flexibility and safety modern apps require.

📄 PDF Download

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

Need a quick reference for your next .NET interview or code review? Download this as a PDF!

💬 Feedback
🚀 Start Learning
Share:

Tags: