Member-only story

Difference between Array and ArrayList in C#

Shahzad Aslam
3 min readFeb 7, 2024

--

Array and ArrayList in C#

Array is related System namespace. Array is used store collections of elements for fixed size. ArrayList is related to System.Collections.Generic. ArrayList is used to store collections of elements for dynamics size.

1. Fixed Size:

Arrays have a fixed size that is determined at the time of creation. Once created, the size cannot be changed.

2. Type Safety:

Arrays are typesafe, which means they’re only intended for holding elements of a given type. The type is determined when the array is declared.

3. Performance:

Compared to ArrayLists, arrays are usually more effective in terms of performance. They have a lower overhead because they are a language-level feature.

4. Multidimensional Arrays:

Arrays can be multidimensional, meaning they can have multiple dimensions (e.g., a two-dimensional array).

5. Direct Access:

An index allows you to easily access elements in an array, enabling rapid random access.

6. Syntax:

Declaration: int[] numbers = new int[5];

7. Syntax:

Accessing an element: int value = numbers[2];

Example

using System;
public class Program
{
public static void Main(string[] args)
{
// Array has fixed size and and specfic data type
int[] mangoPrices = { 10, 20, 30, 40, 50 };
for (int i = 0; i < mangoPrices.Length; i++)
{
Console.WriteLine(mangoPrices[i]);
}
}
}

Output:

10
20
30
40
50

1. Resizable:

ArrayLists can be dynamically resizable. Depending upon the needs, they may increase or decrease in size.

2. Not Type-Safe:

ArrayLists are not type-safe. They can store elements of any type, and type casting is required when retrieving elements.

3. Performance Overhead:

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

No responses yet

Write a response