Array List in C#

By Global Code Factory

Published on:

Array list in C# Net with example showing difference between array and ArrayList in Visual Studio.

Array list in C# .NET is one of the most frequent subjects for beginners and interviewees. A lot of developers mistake it for lists or arrays. Nonetheless, gaining a thorough understanding of array lists will enhance your ability to code and solve problems.

What is an array in C#, With an example?

In C#, an array is a fixed-size grouping of identically typed elements. Its size must be specified during creation.


int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

Above array stores only three integers, and you cannot increase its size once defined. As a result, arrays are less adaptable than array lists.

What is an ArrayList In C#?

ArrayList is part of the System.Collections namespace. It has the ability to store many data kinds. Its size increases dynamically as you add things, unlike arrays.


using System.Collections;

ArrayList list = new ArrayList();
list.Add(10);
list.Add("Hello");
list.Add(true);

An integer, a string, and a boolean are all stored in the ArrayList in this example. Because of its adaptability, it can be helpful in some situations.

Difference Between an Array and an ArrayList in C#

  • ArrayLists expand dynamically, whereas arrays have a fixed size.
  • While an array list can hold various sorts of things, an array only keeps items of one type.
  • While ArrayList gives flexibility, Array delivers superior performance.

// Array
string[] fruits = { "Apple", "Banana", "Mango" };

// ArrayList
ArrayList fruitsList = new ArrayList();
fruitsList.Add("Apple");
fruitsList.Add("Banana");
fruitsList.Add("Mango");
fruitsList.Add(100); // Possible in ArrayList

Difference Between an ArrayList and a List in C#

  • List<T> belongs to System.Collections.Generic and is type-safe.
  • Non-generic array lists are capable of storing a variety of data kinds.
  • Lists perform better due to their robust type-checking.

List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
// Only integers allowed in List<int>

In contrast, ArrayList allows storing different types, which may cause runtime errors.

Important Takeaways

  • In C# .NET, array lists are dynamic and adaptable.
  • Arrays have a finite size yet are type-safe.
  • Developers favor lists in contemporary C# because they are generic and secure.
  • If you need to store a variety of data kinds, use an array list.

Questions and Answers (FAQ)

Q1: What is the purpose of an array list in C# Net?

It is employed to dynamically store mixed-type data without specifying its initial size.

Q2: In C# .NET, what distinguishes an array from an ArrayList?

ArrayLists can contain many kinds and grow dynamically, whereas arrays are fixed and type-specific.

Q3: In C#, which is superior, an array list or a list?

List<T> is superior due to its speed and type safety. Modern programs use array lists less frequently.

Q4: Can ArrayList store objects?

Yes, ArrayList can store objects of any type, including user-defined classes.

Q5: When is it better to utilize an array rather than an array list?

Use arrays when size is fixed, and performance is critical.

Leave a Comment