C# Difference between Array and ArrayList with Examples

  By : Suresh Dasari
  Posted On : 28-Mar-2023

Here we will learn what is array in c#, what is ArrayList in c#, what are the differences between an array and ArrayList in c# with examples.

 

In c#, array and arraylist both are useful to store the collection of elements of the same or different data type elements.

Array in C#

In c#, an array is useful to store the elements of the same data type at contiguous memory locations. The arrays will allow us to store only a fixed number of elements based on the number of items that we mention while defining the array.

Syntax of Array in C#

Following is the syntax of defining an array in c#.

 

type[] sample_array;

Here, the type represents the data type of elements that we store in an array and sample_array represents the name of an array.

C# Array Declaration and Initialization

The following is an example of array declaration and initialization in c#.

 

// Declaring and Initializing an array with a size of 4
int[] array = new int[4];
//Defining and assigning elements at the same time
int[] array2 = new int[3]{1,2,3};
//Initialize with 3 elements will indicate the size of an array
int[] array3 = new int[] { 1, 2, 3};

To learn more about the array functionalities in c# like array initialization, accessing the array elements, looping through the array elements, etc. with examples, check this Arrays in C# with Examples.

ArrayList in C#

Unlike an array in c#, the ArrayList is useful to store the elements of different data types and the size of the arraylist will vary dynamically based on the elements that we add or delete from the arraylist.

Syntax of Defining ArrayList in C#

Following is the syntax of defining the ArrayList in c#.

 

ArrayList arrList = new ArrayList();

If you observe the above ArrayList definition, we created an instance of ArrayList without defining any size.

C# ArrayList Example

The following is an example of declaring and adding elements to an ArrayList in c#.

 

ArrayList arrlist = new ArrayList();
arrlist.Add("Welcome");
arrlist.Add(100);
arrlist.Add(20.5);
arrlist.Add("Tutlane");

To learn more about the ArrayList functionalities in c# check this ArrayList in C# with Examples.

C# Array and ArrayList Differences

As discussed, in c# array and arraylist both are useful to store the collection of the same or different data type elements. The following table lists the main differences between an array and arraylist in c#.

 

ArrayArrayList
The array is a type-safe/strongly typed object which means it will allow only the same data type elements to store. The arraylist is not a type-safe/strongly typed object as it will store elements of any data type.
The array size is fixed and it won't change during runtime. The ArrayList size will vary dynamically based on the adding or removing elements.
To use an array, we need to add a System namespace. To use ArrayList, we need to add System.Collections namespace.
To access array elements, we don't need to cast the elements as the array is strongly typed. To use arraylist elements, we need to cast the elements to the appropriate type.
Accessing elements from an array is faster as it is a strongly-typed object. ArrayList performance is slower because the type casting is necessary to access elements.
The array elements will store in a contiguous/single block of memory. The memory allocation for the ArrayList will vary dynamically as the collection grows.
In c#, the array can have multiple dimensions. The ArrayList will have only one dimension.

As discussed, the array is a fixed-size collection of the same data type elements, whereas an arraylist is a dynamic collection that can store elements of any data type. To know more information about the array and arraylist, visit the c# tutorial