Difference Between Array And ArrayList In C#

Introduction

This blog will give us an idea of the differences between Array and ArrayList in C#, and we can figure out when to use Array vs. ArrayList in C#. Basically, this is an interview question. So this helps beat an interview also.

Array

Arrays are strongly-typed collections of the same data type and have a fixed length that cannot be changed during runtime. We can access the Array elements by numeric index. The array indexes start at zero. The default value of numeric array elements is set to zero, and the reference elements are set to null.

Difference Between Array And ArrayList In C#

Example

Difference Between Array And ArrayList In C#

ArrayList

An Array list is not a strongly-typed collection. It can store the values of different data types or the same datatype. The size of an array list increases or decreases dynamically, so it can take any size of values from any data type. ArrayList is one of the most flexible data structures from C# Collections. ArrayList contains a simple list of values. ArrayList implements the IList interface using an array, and very easily, we can add, insert, delete, view, etc. It is very flexible because we can add without any size information that it will grow dynamically and also shrink.

Example

Difference Between Array And ArrayList In C#

Differences between Array and ArrayList

 
Sr Array ArrayList
1 An array is strongly typed. This means that an array can store only specific types of items\elements ArrayList can store any items\elements.
2 In arrays, we can store only one datatype, either int, string, char, etc… In ArrayList we can store all the datatype values
3 Array cant accept null ArrayList collection accepts null
4 Arrays belong to System.Array namespace
using System;
ArrayList belongs to System.Collection namespaces
using System.Collections;
5 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);
 

When to use Array vs ArrayLists in C#?

Size: Arrays have a fixed size that is determined when they are created, whereas ArrayLists can grow or shrink dynamically based on the number of elements they contain.

Type: Arrays can hold elements of a single data type, whereas ArrayLists can hold elements of any data type.

Performance: Arrays generally perform better than ArrayLists when it comes to accessing elements because array elements are stored in contiguous memory locations, which makes them more efficient to access. ArrayLists, on the other hand, require additional processing overhead to access their elements.

Flexibility: ArrayLists are more flexible than arrays because they can be resized dynamically and can hold elements of different data types.

Overall, the choice between using an array or an ArrayList in C# depends on the specific needs of your program. If you need a fixed-size collection of elements of the same data type, then an array may be the better choice. If you need a dynamic collection that can grow or shrink in size and can hold elements of any data type, then an ArrayList may be a better choice.

Learn more Arrays in C# (Download Sample Project)

ArrayList in C# (c-sharpcorner.com)