array vs arrylist

Arrays are of fixed size. Sometimes, they create some limitations when used in applications. The major limitation is that when we add more number of items to an array than the original size of the array, arrays cannot resize themselves automatically.

However, to resize the array there is a ReDim statement available in vb.net. The ReDim statement resizes the original size of the array. When we execute the ReDim statement, the array loses it's data. So, we use the Preserve keyword along with ReDim statement as shown below.



ReDim Preserve myarray(Ubound(myArray + 1)
This concept is not available in C#.

ArrayList is similar to an array but grows automatically as the number of items are added to the arrayList. We can add, insert and delete items into an ArrayList very easily. And ArrayList stores any type of data inherited from System.Object. In a single Arraylist we can store any data type - integer, string and any type of object inherited from System.Object.

Briefly, an array contains one datatype whereas arraylist can contain many data types, the data in an array is accessed by indexes. Traversing the data in the ararylist can be done by using for..each loop or using enumerators or using indexers.

If you delete an item in an array, it keeps that position empty, whereas in an arraylist that position is occupied by the next element.

Lastly, adding, deleting items in an array is difficult but an arraylist provides methods for adding, deleting and inserting items.

What is the difference between Array and Arraylist? dealt with here on this page - array and arraylist difference