Home » IT » Difference Between ArrayList and Array in C# (With Table)

Difference Between ArrayList and Array in C# (With Table)

ArrayList vs Array in C#

In C#, array and ArrayList are known to be the most used data types. An array is the basic functionality of C# programming language whereas Arraylist in C# is a collection.

Both Array and ArrayList are used to store data and there is no restriction on any data type to be stored.

A major difference between the Array and ArrayList is that array stores data of the same kind of data type whereas array lists can store data of different data types.

For accessing the data from ArrayList, one needs to loop it using the object, whereas in an array you can easily access them without any object.


 

Comparison Table Between ArrayList and Array in C# (in Tabular Form)

Parameter of ComparisonArrayListArray
TypeArrayList is of non-generic typeArray is strongly typed. Only values of the same data type can be stored
Number of elementsIt is dynamic, so any number of data types can be storedOnly fix number of elements can be added
performanceIt degrades the performance since boxing and unboxing is usedIt has better performance.
NULL valueIt can accept NULL valueIt doesn't
ClassIt uses static classIt uses namespace System.collection

 

What is ArrayList in C#?

Array lists are initialized using the list interface. It is considered to be one of the most flexible data in language C3. A collection in programming languages is a special class that stores the data and allows programmers to retrieve.

  1. ArrayList does not have any fixed size, the memory size is dynamic and you can change it when you want. In case, if a coder has initiated memory for 4 elements, then one can add one more element.
  2. In the array list, the size is increased with 2^n value. Also, ArrayList is non-generic, which allows us to store elements of different data types.
  3. Some of the important functions for arraylist are RemoveAt(), Remove(), Insert(), and Add().
  4. Also, it can store the null element.

Example

                                    Using System.collection;

                                    ArrayList  a      = new ArrayList();

                                    a.add(1, “hi”);

                                    a.add(4);

                                    a.add(8.23);

                                    a.add(null);

Some of the functions that are used to interact with data stored in the arraylist.

  1. Add(): it is used to add elements in the ArrayList.
  2. insert(): it is used to insert elements at the specific index in the ArrayList.
  3. Remove(): it is used to remove a single element,
  4. RemoveAt(): it is used to remove specific elements in the ArrayList.
ArrayList in C#
 

What is Array in C#?

Array is a data type in which a programmer can store data of the same type and fixed length. The value of length and data cannot be changed during the runtime. All the array elements are given index value and the array index being with zero.

Hence the default value is zero of the index for the first element in the array.  The syntax for declaring and defining the array in C# is the following. Since the array is a reference data type, its memory is allocated in the heap memory.

                                    Int[] array = new int[] { 10,20,30,40};

                        10   20   30   40   data

                        0     1    2     3       Indices

Some important points about arrays.

  1. Array is of fixed size and it is strongly typed. This means that, for an example, if you make an array of integer values then you can store strings.
  2. Since there is no unboxing or boxing of data, it does give better performance.
RECOMMENDED  Difference Between IPV4 and IPV6 (With Table)
Array in C#

Main Differences Between ArrayList and Array in C#

  1. The size of the array is fixed and it contains the sequential collection of all the same type elements.  Whereas the array list size is not fixed and it increases with the 2^n.
  2. The dimension of the array and each dimension length is initialized when the array is created. During the runtime, it is not possible to change the value of the length of the array. For ArrayList, the value of the length of the array list can be changed dynamically.
  3. The index of the first element in array 0 and the index of the last element is n-1 where n is the length of the size of the array.
  4. ArrayList contains elements of different data types, whereas array contains data of similar data types. For example, if Array is of integer type, then only integers can be stored in the array.
  5. Array is a strongly typed data type and its efficiency is better than the ArrayList. Since in Arraylist unboxing and boxing is needed hence it is less efficient.
  6. ArrayList size increases automatically, hence you need to give any size.

 

Infographic

ArrayList vs Array

 

Conclusion

Array and Arraylist are the datatypes that let the developer store multiple elements of the same datatype in Array and different data types in the array list. Various developers do use Arraylist since Arraylist has different methods that are not there in any other data type such as Repeat(), Reverse(), BinarySearch and many more.

Array is considered to be a fixed size data type and can be accessed easily whereas in ArrayList one needs to use an object and use the dot operator to access the elements stored in the data type.

Well, if a developer needs to store the same type of element and needs to have a fixed size, then it should use an array otherwise, one should use an ArrayList.

Both of them do the same work of storing the elements but the major difference is Fixed Size and dynamic Size. So if you want to store different data types such as null, char, or any other, you should use ArrayList since it is a non-generic data type.

Whereas if you want to restrict a user to insert different data types in a data structure then one should use an array since it lets only the same kind of datatype be inserted in an array.


 

Word Cloud for Difference Between ArrayList and Array in C#

The following is a collection of the most used terms in this article on ArrayList and Array in C#. This should help in recalling related terms as used in this article at a later stage for you.

Word Cloud for ArrayList and Array in C#

 

References

  1. ArrayList Class (System.Collections) | Microsoft Docs
  2. https://www.itu.dk/research/c5/latest/ITU-TR-2006-76.pdf