Array vs Array List
The following table lists the differences between Array and ArrayList in C#.
| Array | ArrayList |
|---|---|
| Must include System namespace to use array. | Must include System.Collections namespace to use ArraList. |
Array Declaration & Initialization:int[] arr = new int[5]int[] arr = new int[5]{1, 2, 3, 4, 5};int[] arr = {1, 2, 3, 4, 5}; | ArrayList Declaration & Initialization:ArrayList arList = new ArrayList();arList.Add(1);arList.Add("Two");arList.Add(false); |
| Array stores a fixed number of elements. The size of an Array must be specified at the time of initialization. | ArrayList grows automatically and you don't need to specify the size. |
| Array is strongly typed. This means that an array can store only specific type of items\elements. | ArrayList can store any type of items\elements. |
| No need to cast elements of an array while retrieving because it is strongly typed and stores a specific type of items only. | The items of ArrayList need to be cast to an appropriate data type while retrieving. So, boxing and unboxing happens. |
| Performs faster than ArrayList because it is strongly typed. | Performs slows because of boxging and unboxing. |
| Use static helper class Array to perform different tasks on the array. | ArrayList itself includes various utility methods for various tasks. |
Comments
Post a Comment