Differrence Between Array and ArrayList in C#

Array ArrayList
Array are strongly typed means their datatype is known at compile time ArrayLists are not strongly typed, they can store value of any datatype
Array are of fixed size Size can increase or decrease dynamically depending on the values stored.
Can store only values of one datatype which is defined during creation. It can store all type of data.
Arrays are faster because they are stongly typed collections. ArrayList are a bit slower as here boxing and unboxing is required.
Namespace : System.Array Namespace : System.Collection
int[] marks = new int[10];
marks[0] = 100;
marks[1] = 200;
…..
…..
ArrayList myArray = new ArrayList();
myArray.Add(1);
myArray.Add(“Seek your career”);
…..
…..
You can access its elements through index. So you can retrieve a particular value directly through the index Here we go sequentialy, we can use ‘for loop’ or foreach method or others to retrieve / traversal.






Let us understand it better.
If you declare an Array with size 2, later if you want to add 1 more value, you cant do that it will throw and exception – “IndexOutOfRangeException”
Array

But this is not the case with ArrayList because it can increase its size at runtime.

Array List
You can see from the above figure at the time of declaration of ArrayList, the size is not specified. Or you can say that it is not fixed. The elements added later will decide the size of the ArrayList.

You can check the size of arraylist with the use of ‘Capacity’ property.
Console.WriteLine(myList.Capacity);

You can also check how many elements are there in the arrayList with the help of ‘Count’ Property.
Console.WriteLine(myList.Count);

There are a number of properties, methods and extension methods provided by both the classes.
You can find more in MSDN Documentation of Array and Array List.

Happy Coding 🙂