| XAML :👈 | 👉:C# QA-1 - 10 |
Array, ArrayList, and List<T> in C# |
Array vs. ArrayList vs. List<T> in C#
1. Overview
In C#, Array, ArrayList, and List<T> are used to store collections of elements. They differ in size flexibility, type safety, performance, and how they handle values of different types.
| Feature | Array | ArrayList | List<T> |
|---|---|---|---|
| Namespace | System |
System.Collections |
System.Collections.Generic |
| Size | Fixed after creation | Can grow or shrink | Can grow or shrink |
| Type safety | Strongly typed | Stores elements as object |
Strongly typed using T |
| Value types | Stored as the specified value type | Boxing may occur | Stored as the specified value type |
| Performance | Efficient for fixed-size collections | May incur boxing and unboxing overhead | Efficient and type-safe for dynamic collections |
| Typical usage | Known, fixed number of elements | Legacy non-generic collections | Most general-purpose dynamic collections |
2. Array in C#
An array stores a fixed number of elements of the same type. Its length is established when the array is created and cannot be changed afterward.
Example
int[] numbers = { 10, 20, 30, 40, 50 };
Console.WriteLine(numbers[0]);
Console.WriteLine(numbers[2]);
Console.WriteLine(numbers.Length);
numbers[1] = 25;
Console.WriteLine(numbers[1]);
Output
10
30
5
25
Explanation
int[]declares an integer array.- Array indexes start at
0. Lengthreturns the number of elements.- You can change an element's value, but you cannot increase or decrease the array's length.
When to use an array
Use an array when the number of elements is known and remains fixed, such as storing the days of the week or a fixed set of configuration values.
3. ArrayList in C#
ArrayList is a non-generic collection in the System.Collections namespace. It can grow or shrink dynamically and stores elements as object, allowing different data types in the same collection.
Example
using System;
using System.Collections;
ArrayList items = new ArrayList();
items.Add(10);
items.Add("Hello");
items.Add(20.5);
Console.WriteLine(items[0]);
Console.WriteLine(items[1]);
Console.WriteLine(items[2]);
Console.WriteLine(items.Count);
Output
10
Hello
20.5
3
Explanation
Add()adds an element to the collection.Countreturns the number of elements currently stored.- Different data types can be stored together because elements are treated as
object. - When retrieving a value as a specific type, casting may be necessary and an invalid cast can cause a runtime exception.
Example of a type-safety problem
ArrayList items = new ArrayList();
items.Add(100);
items.Add("Hello");
int number = (int)items[1];
Result: The last statement throws an InvalidCastException because the second element is a string, not an integer.
Boxing and unboxing
When a value type such as int is stored in an ArrayList, it is boxed into an object. Retrieving it as a value type typically requires unboxing.
ArrayList items = new ArrayList();
items.Add(10); // Boxing
int number = (int)items[0]; // Unboxing
When to use ArrayList
ArrayList is mainly relevant when maintaining older .NET code that already uses non-generic collections. For new C# development, List<T> is generally preferred.
4. List<T> in C#
List<T> is a generic collection in the System.Collections.Generic namespace. It grows or shrinks dynamically while enforcing the element type specified by T.
For example, List<int> stores integers, while List<string> stores strings.
Example
using System;
using System.Collections.Generic;
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
Console.WriteLine(numbers[0]);
Console.WriteLine(numbers.Count);
numbers.Add(40);
numbers.Remove(20);
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Output
10
3
10
30
40
Explanation
List<int>ensures that elements are integers.Add()adds elements dynamically.Remove()removes a matching element.Countreturns the current number of elements.foreachiterates through the collection.- Adding a string directly to
List<int>causes a compile-time error.
When to use List<T>
Use List<T> for most general-purpose collections when the number of elements can change and you want type safety. It is commonly used to store customers, employees, products, and other application data.
5. Important Differences in Practice
Array: fixed size
int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
// numbers[3] = 40; // IndexOutOfRangeException
The array can store only three elements. Accessing index 3 is outside its valid range.
List<T>: dynamic size
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
numbers.Add(40);
Console.WriteLine(numbers.Count);
Output:
4
The list automatically manages its internal storage as elements are added. Its capacity may grow, while its Count represents the number of elements currently stored.
6. Which One Should You Choose?
- Array: Choose when the collection length is fixed and you want straightforward, efficient indexed access.
- ArrayList: Generally avoid for new development; use it mainly when working with legacy APIs that require it.
- List<T>: Choose for most dynamic collections because it combines flexible sizing with compile-time type safety.
7. Interview Answer
An array has a fixed length and stores elements of a specified type. An ArrayList is a dynamically sized, non-generic collection that stores elements as objects, so it can require casting and boxing or unboxing. A List<T> is a dynamically sized generic collection that provides type safety and efficient collection operations. In modern C# applications, List<T> is generally preferred when a collection needs to grow or shrink.
Explain the Difference Between Array, ArrayList and List<T>.
All three can store collections of values, but they differ in size management and type safety.
| Feature | Array | ArrayList | List<T> |
|---|---|---|---|
| Namespace | System |
System.Collections |
System.Collections.Generic |
| Size | Fixed after creation | Dynamically grows or shrinks | Dynamically grows or shrinks |
| Type Safety | Elements have a defined array element type | Stores elements as object |
Stores elements of type T |
| Value-Type Handling | Stores values as its element type | Value types are boxed when stored as object |
Stores values as T, avoiding collection-level boxing for value types |
| Performance | Efficient for fixed-size collections | May incur boxing and casting overhead | Generally preferred for strongly typed, resizable collections |
| Modern Usage | Useful for fixed-size data | Legacy or interoperability scenarios | Recommended for most resizable, strongly typed collections |
Array Example
int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
The array length is fixed at three elements.
ArrayList Example
using System.Collections;
ArrayList items = new ArrayList();
items.Add(10);
items.Add("Hello");
int number = (int)items[0];
An ArrayList can store different object types, but retrieving values often requires casting and may cause runtime errors if the cast is invalid.
List<T> Example
using System.Collections.Generic;
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
int number = numbers[0];
List<int> stores integers, provides compile-time type safety, and can grow as needed.
Interview summary: Use an array for fixed-size collections, ArrayList mainly when maintaining legacy code that requires it, and List<T> for most modern, resizable, strongly typed collections.
| XAML :👈 | 👉:C# QA-1 - 10 |