ArrayList vs Array in C#: Difference and Comparison

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

Key Takeaways

  1. ArrayList is a dynamic, resizable collection that can store elements of any data type, automatically adjusting its size as elements are added or removed. However, it may have a performance cost due to the boxing and unboxing of value types.
  2. Arrays in C# are fixed-size, strongly-typed collections that can store elements of a specific data type, offering better performance due to the elimination of boxing and unboxing but requiring a predefined size upon initialization.
  3. The primary difference between ArrayList and Array in C# is the flexibility and type safety, with ArrayList offering dynamic resizing and storage of multiple data types. In contrast, arrays provide better performance and type safety but have a fixed size.

ArrayList vs Array in C#

ArrayList is a dynamic list of objects that can grow or shrink, while an array has a fixed size. ArrayList has methods for data manipulation, like adding or removing elements, while an array requires manual resizing. ArrayList can store elements of different data types, while an array can store elements of the same data type.

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.

ArrayList vs Array in C

 

Comparison Table

Parameter of ComparisonArrayListArray
TypeArrayList is of non-generic typeAn array 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 a fixed number of elements can be added.
performanceIt degrades the performance since boxing and unboxing are used.It has better performance.
NULL valueIt can accept a NULL valueIt doesn’t
ClassIt uses static classIt uses a namespace System. collection

 

What is ArrayList in C#?

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

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

Example

                                    Using System. collection;

                                    ArrayList  a      = new ArrayList();

                                    a.add(1, “hi”);

                                    a.add(4);

                                    a.add(8.23);

                                    a.add(null);

Some functions 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 removes specific elements in the ArrayList.
 

What is Array in C#?

An 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 values, and the array index is 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. An array is of fixed size, and it is strongly typed. This means that, for example, if you make an array of integer values, then you can’t store strings.
  2. Since there is no unboxing or boxing of data, it performs better.

Main Differences Between ArrayList and Array in C#

  1. The array size is fixed and contains the sequential collection of all the same type elements.  The array list size is not fixed and increases with the 2^n.
  2. The dimension of the array and each dimension length is initialized when the array is created. Changing the value of the array’s length is impossible during the runtime. For ArrayList, the value of the length of the array list can be changed dynamically.
  3. The index of the first element in the array is 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 an 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. An array is a strongly typed data type, and its efficiency is better than the ArrayList. Since in Arraylist, unboxing and boxing is needed, it is less efficient.
  6. ArrayList size increases automatically; hence you need to give any size.
Difference Between X and Y 43
References
  1. ArrayList Class (System.Collections) | Microsoft Docs
  2. https://www.itu.dk/research/c5/latest/ITU-TR-2006-76.pdf
Also Read:  How To View and Download Apple Card Statements on Mac: A Quick Guide

Last Updated : 11 June, 2023

dot 1
One request?

I’ve put so much effort writing this blog post to provide value to you. It’ll be very helpful for me, if you consider sharing it on social media or with your friends/family. SHARING IS ♥️

25 thoughts on “ArrayList vs Array in C#: Difference and Comparison”

  1. While the comparison is insightful, I feel that the article doesn’t delve into the potential trade-offs when choosing between ArrayList and Array.

    Reply
  2. Informative article! I’ve always had trouble understanding the differences between array and ArrayList, but this clarified it perfectly.

    Reply
  3. While it’s important to understand the differences, I also think it’s crucial to know when to use ArrayList over arrays and vice versa. Context is key.

    Reply
  4. I appreciate that the article not only highlights differences but also provides practical examples. It enhances the understanding of the concepts.

    Reply
    • I respectfully disagree. While the concepts may be clear to you, the examples can really benefit those who need extra assistance in understanding.

      Reply
  5. Great explanation of the differences between ArrayList and Array in C#! I appreciate the clear comparison of their performance and flexibility.

    Reply
  6. The detailed functions for both ArrayList and Array provided in this article are incredibly helpful. It’s like having a quick reference guide.

    Reply
  7. The thorough comparison between ArrayList and Array in C# is excellent, but I’d love to see more practical use cases for each.

    Reply
  8. I disagree with the preference for arrays over ArrayList due to better performance. The flexibility of ArrayList can be invaluable in certain situations.

    Reply
    • I think both arrays and ArrayList have their own strengths and weaknesses, and it’s important to consider the specific requirements of each programming task.

      Reply
    • I see your point, but there are also cases where the fixed size of arrays is more suitable. It’s all about choosing the right tool for the job.

      Reply

Leave a Comment

Want to save this article for later? Click the heart in the bottom right corner to save to your own articles box!