Array vs ArrayList


In this article we are going to discuss difference between Array and ArrayList in C#. There are many blogs, articles are available on the internet regarding it but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Array and ArrayList in C#.

Difference Between Array and ArrayList in C#

S.No. Array ArrayList
1 Arrays are strongly typed. This means that once you declare an array of a specific type, it can only store items of that type.
Array in C#
ArrayLists are not strongly typed (they are loosely typed). They can store any type of objects as they are of type data.
Array in C#
2 Arrays have a fixed size. You must specify the size when you create an array, and it cannot be changed.
Array in C#
ArrayLists are dynamic and can grow or shrink in size as needed.
Array in C#
3 Less flexible due to fixed size and strong typing. More flexible because of dynamic sizing and ability to store different types.
4 Use when the number of elements is known and fixed, and type safety is important. Use when a collection of objects needs to be stored, and flexibility in size and types is required.
5 Because arrays are strongly typed, they provide better performance. ArrayLists, being loosely typed, involve boxing and unboxing when storing and retrieving value types, which can lead to performance overhead.
6 Example
int[] numbers = new int[3];
numbers[0] = 1;
numbers[1] = 2;
Example
ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add("string");

Next