Arrays: An array is a group of like-typed variables that are referred to by a common name. Example:
CSHARP
using System;
class GFG {
public static void Main(string[] args)
{
int[] arr = new int[4];
arr[0] = 47;
arr[1] = 77;
arr[2] = 87;
arr[3] = 97;
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
}
}
|
To read more about arrays please refer C# | Arrays ArrayList: ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. Example:
CSHARP
using System;
using System.Collections;
class GFG {
public static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add("Ajay");
al.Add("Ankit");
al.Add(10);
al.Add(10.10);
foreach(var names in al)
{
Console.WriteLine(names);
}
}
}
|
Output:
Ajay
Ankit
10
10.1
To read more about ArrayList please refer C# | ArrayList Class
Difference Between Array and ArrayList
| Feature |
Array |
ArrayList |
| Memory |
This has fixed size and can’t increase or decrease dynamically. |
Size can be increase or decrease dynamically. |
| Namespace |
Arrays belong to System.Array namespace |
ArrayList belongs to System.Collection namespace. |
| Data Type |
In Arrays, we can store only one datatype either int, string, char etc. |
In ArrayList we can store different datatype variables. |
| Operation Speed |
Insertion and deletion operation is fast. |
Insertion and deletion operation in ArrayList is slower than an Array. |
| Typed |
Arrays are strongly typed which means it can store only specific type of items or elements. |
ArrayList are not strongly typed. |
| null |
Array cannot accept null. |
ArrayList can accepts null. |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!