C# Collections: Understanding ArrayList
Quick Answer
ArrayList in C# is a non-generic collection that stores objects dynamically. It allows adding, removing, and accessing elements without specifying a fixed size. While useful for storing heterogeneous objects, it is less type-safe compared to generic collections like List<T>.
Learning Objectives
- Explain the purpose of ArrayList in a practical learning context.
- Identify the main ideas, terms, and decisions involved in ArrayList.
- Apply ArrayList in a simple real-world scenario or practice task.
Introduction to ArrayList in C#
In C#, collections are essential for storing and managing groups of objects. One such collection is the ArrayList.
ArrayList is a dynamic array that can hold objects of any type, growing as needed during runtime.
ArrayList provides flexibility by allowing dynamic resizing and heterogeneous object storage.
What is ArrayList?
ArrayList is a collection class in the System.Collections namespace that stores elements as objects.
Unlike arrays, ArrayLists can dynamically resize, meaning you do not need to specify the size upfront.
- Stores elements as objects (non-generic).
- Can hold different types of objects together.
- Automatically resizes as elements are added or removed.
Creating and Using an ArrayList
To use ArrayList, you first need to include the System.Collections namespace.
You can add, remove, and access elements using methods and indexers.
- Create an ArrayList instance.
- Add elements using Add() method.
- Access elements by index.
- Remove elements using Remove() or RemoveAt().
Example: Basic ArrayList Operations
The following example demonstrates creating an ArrayList, adding elements, and iterating through them.
Advantages and Limitations of ArrayList
ArrayList offers flexibility but also has some drawbacks compared to generic collections.
- Advantages:
- - Dynamic resizing without manual intervention.
- - Can store heterogeneous objects.
- Limitations:
- - Stores elements as objects, requiring casting when retrieving.
- - Less type-safe, increasing risk of runtime errors.
- - Generally slower than generic collections due to boxing/unboxing.
When to Use ArrayList
ArrayList is useful when you need a dynamically sized collection that can hold different types of objects.
However, for type safety and performance, generic collections like List<T> are preferred in modern C# development.
- Use ArrayList for legacy code compatibility.
- Use when object types vary and cannot be generalized.
- Prefer generic collections for new development.
Practical Example
This example creates an ArrayList, adds different types of elements, and prints each element to the console.
Examples
using System;
using System.Collections;
class Program
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add(10);
list.Add("Hello");
list.Add(3.14);
foreach (object item in list)
{
Console.WriteLine(item);
}
}
}This example creates an ArrayList, adds different types of elements, and prints each element to the console.
Best Practices
- Prefer generic collections like List<T> for type safety and better performance.
- Use ArrayList only when you need to store heterogeneous objects.
- Always cast elements retrieved from ArrayList to the expected type carefully.
- Avoid excessive boxing/unboxing by minimizing use of value types in ArrayList.
Common Mistakes
- Assuming ArrayList is type-safe and skipping type casting.
- Using ArrayList in new code instead of generic collections.
- Not handling invalid casts leading to runtime exceptions.
- Ignoring performance implications of boxing/unboxing.
Hands-on Exercise
Create and Manipulate an ArrayList
Write a C# program that creates an ArrayList, adds at least five elements of different types, removes one element, and prints all remaining elements.
Expected output: Console output listing all elements except the removed one.
Hint: Use Add(), Remove(), and foreach loop to iterate.
Interview Questions
What is an ArrayList in C#?
InterviewArrayList is a non-generic collection class in C# that stores elements as objects and can dynamically resize.
Why might you prefer List<T> over ArrayList?
InterviewList<T> is type-safe, avoids boxing/unboxing overhead, and provides better performance compared to ArrayList.
What is ArrayList, and why is it useful?
BeginnerArrayList in C# is a non-generic collection that stores objects dynamically.
MCQ Quiz
1. What is the best first step when learning ArrayList?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce ArrayList?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. ArrayList in C# is a non-generic collection that stores objects dynamically.
B. ArrayList never needs examples
C. ArrayList is unrelated to practical work
D. ArrayList should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- ArrayList in C# is a non-generic collection that stores objects dynamically.
- It allows adding, removing, and accessing elements without specifying a fixed size.
- While useful for storing heterogeneous objects, it is less type-safe compared to generic collections like List<T>.
- In C#, collections are essential for storing and managing groups of objects.
- ArrayList is a dynamic array that can hold objects of any type, growing as needed during runtime.
Summary
ArrayList is a flexible, dynamically sized collection that stores objects in C#.
It allows heterogeneous data storage but lacks type safety and can incur performance costs.
Modern C# development favors generic collections like List<T> for most scenarios.
Frequently Asked Questions
Is ArrayList type-safe?
No, ArrayList stores elements as objects, so you must cast them to the correct type when retrieving, which can lead to runtime errors.
Can ArrayList store different types of objects?
Yes, ArrayList can store heterogeneous objects because it stores elements as type object.
Why is List<T> preferred over ArrayList?
List<T> provides compile-time type safety, better performance, and avoids boxing/unboxing overhead.
What is ArrayList?
ArrayList in C# is a non-generic collection that stores objects dynamically.
Why is ArrayList important?
It allows adding, removing, and accessing elements without specifying a fixed size.
How should I practice ArrayList?
While useful for storing heterogeneous objects, it is less type-safe compared to generic collections like List<T>.

