What is the difference between Arraylist and Array? | C# interview Questions 27

An array holds items of the same type and has a fixed size. An ArrayList can hold items of different types and does not have a fixed size.

Array:

Fixed Size: Size is set at the time of creation and cannot be changed.
Type-Safe: Stores elements of a specific type.
Performance: Faster access due to type safety and fixed size.
Declaration: int[] numbers = new int[5];
ArrayList:

Dynamic Size: Can grow and shrink dynamically as needed.
Non-Type-Safe: Stores elements as objects, requiring casting for specific types.
Performance: Slower due to boxing/unboxing and dynamic resizing.
Declaration: ArrayList list = new ArrayList();
Key Difference: Arrays have a fixed size and are type-safe, while ArrayLists are dynamic in size and can store any type of objects.

Leave a Reply

Your email address will not be published. Required fields are marked *