---
title: "Difference between Array and ArrayList"  
description: "Difference between Array and ArrayList"  
author: "Sandra Emily"  
published: 2024-06-13  
updated: 2024-06-13  
canonical: https://www.mindstick.com/forum/160742/difference-between-array-and-arraylist  
category: "c#"  
tags: ["c#", "collection", "array", "array list"]  
reading_time: 3 minutes  

---

# Difference between Array and ArrayList

[Difference between Array](https://www.mindstick.com/forum/34335/what-is-difference-between-array-and-arraylist) and [ArrayList](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp)

## Replies

### Reply by Ashutosh Patel

### C# Array vs ArrayList

Arrays and ArrayLists are used to store aggregated data but have many differences in features and functionality

## Syntax-

## [Array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net)

```cs
int[] arrayName = new int[size];
```

## ArrayList

```cs
ArrayList arrayListName = new ArrayList();
```

There are several differences between Array and ArrayList as follows,

#### Type Safety

- **Arrays** are type-safe, meaning you specify the type of elements the array can contain when you declare them. Once declared, an array can only contain elements of its specified type.
- **ArrayLists** are not type-safe; You can contain elements of any data type. These variables can cause runtime errors if you try to store incompatible types in an ArrayList.

#### Resizing

- **Arrays** have a fixed size when created. If you need to change the size of an array, you must create a new array of the desired size and copy the elements from the old array to the new array.
- **ArrayLists** dynamically resize themselves as needed. They automatically grow or shrink in proportion to the amount of material added or subtracted.

#### Performance

- **Arrays** typically offer better performance than ArrayLists because they have a fixed size and direct access to elements via indexes.
- **ArrayList-** If capacity needs to change, there may be some performance overhead due to resizing the ArrayLists process.

#### Usage

- **Array-** If the size of the collection is already known, the arrays are likely to be used extensively and not modified.
- **ArrayLists** are used when column size can change or when element type flexibility is needed.

#### Length Property vs. Count Property

- **Arrays** use the **Length** property to get the number of elements in an Array.

```cs
int length = arrayName.Length;
```

- **ArrayLists** use the **Count** property to get the number of elements in an ArrayList.

```cs
int count = arrayListName.Count;
```

####

#### Element Access

- **Arrays** provide direct access to elements through **indexing**.

```cs
elementType element = arrayName[index];
```

- **ArrayLists** Provides access to objects using the **ArrayList[index]** syntax or through methods such as **Add**, **Insert**, and **Remove**.

```cs
object element = arrayListName[index];
```

## Example-

## Array

```cs
using System;

class Program
{
    static void Main()
    {
        // Declaring and initializing an array
        int[] numbers = { 1, 2, 3, 4, 5 };

        // Accessing elements from array
        Console.WriteLine("First element: " + numbers[0]);
        Console.WriteLine("Second element: " + numbers[1]);

        // Iterating through loop from array
        Console.WriteLine("\nArray elements are:");
        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }
        Console.ReadLine();
    }
}
```

## Output-

```plaintext
First element: 1
Second element: 2
Array elements are:
1
2
3
4
5
```

## ArrayList

```cs
using System;
using System.Collections;
class Program
{
   static void Main()
   {
       // Creating an ArrayList
       ArrayList arrayList = new ArrayList();
       // Adding elements in the ArrayList
       arrayList.Add("Apple");
       arrayList.Add("Banana");
       arrayList.Add("Orange");
       // Accessing elements from the ArrayList
       Console.WriteLine("First element: " + arrayList[0]);
       Console.WriteLine("Second element: " + arrayList[1]);
       // Iterating through the loop from ArrayList
       Console.WriteLine("\nArrayList elements:");
       foreach (object item in arrayList)
       {
           Console.WriteLine(item);
       }
       Console.ReadLine();
   }
}
```

## Output-

```plaintext
First element: Apple
Second element: Banana
ArrayList elements:
Apple
Banana
Orange
```

In summary, Arrays work well and are suitable for **fixed-size** collections of known types, while ArrayLists offer **flexibility** and **dynamic resizability**, making them suitable for situations where collection size may change or where type flexibility is important However, with the introduction of generics in C# (e.g., **List<T>**), **ArrayLists** are rare in modern codebases.

**Also, Read:** [Dictionary vs Hashtable in C#](https://www.mindstick.com/forum/160743/difference-between-hashtable-and-dictionary)


---

Original Source: https://www.mindstick.com/forum/160742/difference-between-array-and-arraylist

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
