What is an Array
An array is a sequential collection of similar data that can be accessed as per the “index”. It is the simplest type of data structure in which the elements get stored in a contiguous memory location.
In Array, index starts at zero, so to access the first element of an array “numarray”, it should be written as numarray[0].
Example of Array in C#
Output:-
10 20 30 40 50
What is an ArrayList
The ArrayList is a collection of objects of same or different types. The size of an ArrayList can be dynamically increased or decreased as per the requirement. It works like an array but unlike array in ArrayList items can be dynamically allocated or deallocated, i.e you can add, remove, index, or search for data in a collection.
Example of ArrayList in C-Sharp
Output:-
123 abc 67 pqr 45
Difference Between Array and ArrayList
- Array stores data of the same type whereas ArrayList stores data in the form of the object which may be of different types.
- Size of an ArrayList grows dynamically while Array size remains static throughout the program.
- Insertion and deletion operation in ArrayList is slower than an Array.
- Arrays are strongly typed whereas ArrayLists are not strongly typed.
- Arrays belong to System.Array namespace whereas ArrayList belongs to System.Collections namespace.
- When choosing between Array and ArrayList, decide on the basis of their features that you want to implement.
luqmaan s says
Great explanation! Arrays are rigid and fixed, while ArrayLists offer flexibility. Understanding the differences is crucial for efficient data handling in C#. Thanks for clarifying!