Difference Between ArrayList and Array in C#

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.

/10

IT Quiz

Test your knowledge about topics related to technology

1 / 10

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

2 / 10

Who is considered as the father of computing

3 / 10

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

4 / 10

Saving a file from the Internet onto your desktop is called

5 / 10

Geo-stationary satellite revolves at –

6 / 10

Which of the following is defined as an attempt to steal, spy, damage or destroy computer systems, networks, or their associated information?

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

8 / 10

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

9 / 10

Which of the following semiconductor is mostly used to construct electronic circuits?

10 / 10

While making the text bold in Word, what do you need to do first?

Your score is

0%

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

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


 

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.
References
  1. ArrayList Class (System.Collections) | Microsoft Docs
  2. https://www.itu.dk/research/c5/latest/ITU-TR-2006-76.pdf
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 ♥️

Leave a Comment

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