Top 9 differences between Array and ArrayList

Top 9 differences between Array and ArrayList in C#

Top 9 differences between Array and ArrayList 

Array ArrayList
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.
Array stores fixed number of elements. Size of an Array must be specified at the time of initialization. ArrayList grows automatically and you don’t need to specify size.
No need to cast elements of an array while retriving because it is strongly type and stores specific type of items only. Items of ArrayList need to be cast to appropriate data type while retriving.
Use static helper class Array to perform different tasks on the array. 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.
In arrays we can store only one datatype. It’s generic type of collection In ArrayList we can store all the datatype values. It’s non-generic type of collection
Array cant accept null. ArrayList collection accepts null.
Array provides better performance than ArrayList as no boxing and unboxing required because an array stores the same type of data. 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..
Arrays belong to System.Array namespace using System; Arraylist belongs to System.Collection namespaces using System.Collections;
Example – int[] intArray=new int[]{2}; intArray[0] = 1; intArray[2] = 2; Example – ArrayList Arrlst = new ArrayList(); Arrlst.Add(“Sagar”); Arrlst.Add(1); Arrlst.Add(null);

Let’s take some example of Array

// creating array 
int[] arr = new int[4]; 
  
// initializing array 
arr[0] = 47; 
arr[1] = 77; 
arr[2] = 87; 
arr[3] = 97; 

// traversing array 
for (int i = 0; i < arr.Length; i++) 
{ 
   Console.WriteLine(arr[i]);
}

Let’s take some example of ArrayList

// Create a list of strings 
ArrayList al = new ArrayList(); 
al.Add("Dhruv"); 
al.Add("Pole"); 
al.Add(50); 
al.Add(10.68); 
  
// Iterate list element using foreach loop 
foreach(var names in al) 
{ 
    Console.WriteLine(names); 
}
So I have explained regarding Top 9 differences between Array and ArrayList in C#. Kindly share your feedbacks about differences between Array and ArrayList in C#,if I have missed. Folks you should also refer below articles for better understanding of differences between:

Leave a Reply