Important Differences Between Array and ArrayList in C#

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[5]; 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[0] = 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();

list.Add(“Hello”);

list.Add(1);

list.Add(3.14);

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#

  1. Data Type: Arrays can only hold elements of a specific data type, whereas ArrayList can hold any type of data as it stores objects.
  2. 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.
  3. 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>
  4. 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.
  5. 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.
  6. Methods: ArrayList has many useful methods like Add, Remove, and Capacity, whereas arrays don’t have such methods.
  7. 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.
  8. Compile-time Checking: Arrays are checked at compile-time, while ArrayList is checked at run-time.
  9. Access: Array elements can be accessed using their index, whereas ArrayList elements can be accessed using an indexer or an enumerator.

Leave a Reply

error: Content is protected !!