Array and Arraylist in C#
An array stores a fixed-size sequential collection of elements of the same type. The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance. Arrays are zero indexed: an array with n elements is indexed from 0 to n-1.
The ArrayList class is designed to hold heterogeneous collections of objects. However, it does not always offer the best performance. Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based. The ArrayList collection accepts null as a valid value. It also allows duplicate elements.
Array Vs ArrayList
They are different object types. Arrays belong to System.Array namespace whereas Arraylist belongs to System.Collection namespaces .
Array is strongly typed . This means that an array can store only specific type of items\elements. As a result, it is type safe, and is also the most efficient, both in terms of memory and performance. While in arraylist, we can store all the datatype values.
Array stores fixed number of elements. Size of an Array must be specified at the time of initialization . ArrayList grows automatically and you don't need to specify size.
- What are the Default Access Modifiers in C#?
- What is a sealed class in C#?
- What is operator overloading in c#?
- Difference Between Finalize and Dispose Method in C#?
- What is an Object Pool in .Net?
- What is Anonymous type in C#?
- Dynamic type variables Vs Object type variables in C#?
- Difference Between Clone() and CopyTo() Array
- Difference between throw and throw new Exception() in C# ?
- What is the use of finally block in C#?