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.
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.
Following is the syntax of defining an array in c#.
Here, the type represents the data type of elements that we store in an array and sample_array represents the name of an array.
The following is an example of array declaration and initialization in c#.
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.
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.
Following is the syntax of defining the ArrayList in c#.
If you observe the above ArrayList definition, we created an instance of ArrayList without defining any size.
The following is an example of declaring and adding elements to an ArrayList in c#.
To learn more about the ArrayList functionalities in c# check this ArrayList in C# with Examples.
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#.
Array | ArrayList |
---|---|
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.