Recently updated on August 20th, 2023 at 11:44 am
Array in C#
An array in C# is a collection of elements of the same data type that are stored in contiguous memory locations. The elements can be accessed by their index, which is an integer value that represents the position of the element within the array. Arrays are declared by specifying the type of the elements followed by the name of the array and the size of the array in square brackets. For example, int numbers = new int; declares an array of integers called “numbers” with a size of 5. Elements can be accessed and assigned values using the array name and the index of the element in square brackets, for example, numbers = 1; assigns the value 1 to the first element of the array.
ArrayList in C#
An ArrayList in C# is a collection class that implements the IList interface. It is similar to an array, but it can grow or shrink in size dynamically. ArrayList stores objects and can hold any type of data, unlike arrays, which can only hold elements of a specific data type.
ArrayList has a property called Capacity which tells the current size of the ArrayList. It also has a method called Add which can be used to add elements to the ArrayList.
Here is an example of creating an ArrayList and adding elements to it:
ArrayList list = new ArrayList();
ArrayList also has a method called Remove, which can be used to remove an element from the list. And it has an indexer so that you can access the elements of the ArrayList using the indexer like an array.
Keep in mind that ArrayList is not the best choice when performance is a concern, because it has a higher overhead than other collections. In those cases, collections like List<T> or LinkedList<T> are better options.
Important Differences Between Array and ArrayList in C#
- Data Type: Arrays can only hold elements of a specific data type, whereas ArrayList can hold any type of data as it stores objects.
- Size: The size of an array is fixed and cannot be changed once it is created. On the other hand, the size of an ArrayList can be changed dynamically as it can grow or shrink in size.
- Performance: Arrays are more efficient than ArrayList when it comes to performance because they have less overhead. ArrayList has a higher overhead than other collections like List<T> or LinkedList<T>
- Memory: ArrayList uses more memory than an array because it needs to keep track of the size of the collection, the capacity, and the elements themselves.
- Syntax: Array have a different syntax than ArrayList. Array have its size defined in the square brackets  and ArrayList doesn’t have any size defined.
- Methods: ArrayList has many useful methods like Add, Remove, and Capacity, whereas arrays don’t have such methods.
- Type Safety: Arrays are type safe, which means that you can only store elements of a specific data type, but ArrayList is not type safe.
- Compile-time Checking: Arrays are checked at compile-time, while ArrayList is checked at run-time.
- Access: Array elements can be accessed using their index, whereas ArrayList elements can be accessed using an indexer or an enumerator.