Why are array and ArrayList difference when would you use each?

They are different object types. They have different capabilities and store their data in different ways.

An Array [System.Array] is fixed in size once it is allocated. You can't add items to it or remove items from it. Also, all the elements must be the same type. As a result, it is type safe, and is also the most efficient of the three, both in terms of memory and performance. Also, System.Array supports multiple dimensions [i.e. it has a Rank property] while List and ArrayList do not [although you can create a List of Lists or an ArrayList of ArrayLists, if you want to].

An ArrayList is a flexible array which contains a list of objects. You can add and remove items from it and it automatically deals with allocating space. If you store value types in it, they are boxed and unboxed, which can be a bit inefficient. Also, it is not type-safe.

A List leverages generics; it is essentially a type-safe version of ArrayList. This means there is no boxing or unboxing [which improves performance] and if you attempt to add an item of the wrong type it'll generate a compile-time error.

Video liên quan

Chủ Đề