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
/10

IT Quiz

Test your knowledge about topics related to technology

1 / 10

Which number system has a base 16

2 / 10

Who founded Microsoft?

3 / 10

For which of the following Android is mainly developed?

4 / 10

The output printed by a computer through a printer on the paper is called

5 / 10

The main function of smart assistants like Apple Siri and Amazon Alexa is

6 / 10

A process that is repeated, evaluated, and refined is called __________

7 / 10

Who is considered as the father of computing

8 / 10

When a machine possesses the ability to mimic human traits like make decisions, predict the future, learn and improve on its own said to have

9 / 10

Which of these is not a social media platform?

10 / 10

Systems for differently-abled individuals is an example of

Your score is

0%


 

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.

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

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.

    1. It’s always important to consider the pros and cons of different data types, and that would be a valuable addition to this article.

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

    1. It’s great when complex concepts are explained in a straightforward manner. I found this article very helpful as well.

  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.

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

  5. I find the use of practical examples in the article a bit redundant. The concepts are already clear without them.

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

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

  7. The comparison table makes it easy to understand the distinctions between ArrayList and Array. This format is very effective!

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

    1. Absolutely, having all the necessary functions laid out clearly saves time when working with ArrayList and Array.

  9. Avatar of Griffiths Connor
    Griffiths Connor

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

    1. Agreed, having real-world examples would make it easier to understand the best applications for ArrayList and Array.

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

    1. 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.

    2. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

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