In this article, we will see the basic differences between an Array and an ArrayList.
C# Array
An Array is a collection of data items of the same type. An Array is reference type so memory for the array is allocated on the heap. We can initialize an Array using the "new" operator and by specifying the type and number of elements inside the Array.
Example
string[] array1=new string[5];
As memory for an Array is allocated on the heap, 5 empty array elements are allocated on the heap when we initialize the Array1.
After array is declared and initialized, you can access the array elements using indexer. Array support only indexers having integer parameters.
array1[0]=”Hello”;
array1[1]=”Bye”;
- Here, the first important point to note is that array has a fixed size.
- An array is strongly-typed. It means if we declare an Array of string type, then we cannot store the integer value in that array.
- The array provides better performance than the ArrayList because an array stores the same type of data which doesn't need unnecessary boxing or unboxing.
- "Array class" is the base class for all arrays in C#. It is defined in system namespace.
- In an array we cannot store null.
C# ArrayList
ArrayList implements the IList interface. ArrayList is one of the most flexible data structures from C# collection. Collection classes are special classes for data storage and retrieval.
Example
using System.Collection;
ArrayList a1 = new ArryList();
a1.add(null);
a1.insert(1, ”hi”);
a1.add(3);
a1.add(8.23);
- ArrayLists do not have a specific size. When we initialize an arraylist, it will initially allocate the memory for 4 elements. When we add the 5th element, ArrayList will automatically redimension to double of its current size. So, the size will increase as 4, 8, 16, 32, 64 and so on (i.e 2^n).
- ArrayList is a non-generic type of collection in C#. It means you can store any type of data in ArrayList.
- ArrayList provides the facility of dynamic size but it comes at a cost of performance. The ArrayList's internal Array is of "object type". So, if we are using value type then each element is boxed and stored on a heap and whenever we access them it is unboxed to value type.
- ArrayList implements the IList interface so, it provides a various method that we can use for easy implementation.
- Add() - Add single element to ArrayList.
- Insert() - Allow inserting a single element at the specific position.
- Remove() - Remove single elemet from ArrayList.
- RemoveAt() - Remove element from specific postion.
- To use ArrayList you must add a namespace System.Collection.
- ArrayList can store null.
Differences between an Array and an ArrayList
Array |
ArrayList |
An Array is strongly-typed. We can store only the same type of data. |
ArrayList is a non-generic collection type. ArrayList's internal Array is of the object type. So, we can store multiple types of data in ArrayList. |
Array stores a fixed number of elements. |
ArrayList is dynamic in term of capacity. If the number of element exceeds, ArrayList will increase to double its current size. |
Array provides better performance than ArrayList. |
If we are using a large number of ArrayList then it degrades performance because of boxing and unboxing. |
Array uses static helper class Array which belongs to system namespace |
ArrayList implements an IList interface so, it provides a method that we can use for easy implementation. |
Array belongs to namespace System |
ArrayList belongs to namespace System.Collection |
The Array cannot accept null. |
An Array can accept null. |
Example:
string[] array1=new string[5];
array1[0]=”Hello”;
array1[1]=”Bye”; |
Example:
ArrayList a1=new ArryList();
a1.add(null);
a1.insert(1,”hi”);
a1.add(3);
a1.add(8.23); |