1.

Array vs ArrayList in C#

Answer»

Some of the major differences between an Array and an ARRAYLIST in C# are given as FOLLOWS:

Array
ArrayList
A group of similar type variables that can be referred by a common name is known as an Array.The ArrayList is a COLLECTION that implements the IList Interface and uses an array that is dynamic.
The System.Array namespace contains Arrays.
The System.Collection namespace contains ArrayList.
An Array cannot accept a NULL value.
An ArrayList can accept a null value.
An Array has a fixed SIZE and this size cannot be increased or decreased dynamically.
The size of an ArrayList can be increased or decreased dynamically.
An Array can store data of a single data type such as int, float, char etc.
An ArrayList can store data of different data types.
The insertion and deletion operation in an Array is quite fast.
The insertion and deletion operation in an ArrayList is slower as compared to an Array.
An Array is strongly typed.
An ArrayList is not strongly typed.

A program that demonstrates an Array and ArrayList in C# is given as follows:

using System; using System.Collections; namespace Demo {   class Example   {      static void Main(string[] args)      {         int[] arr = new int[5];         arr[0] = 4;         arr[1] = 7;         arr[2] = 3;         arr[3] = 9;         arr[4] = 1;         Console.Write("Array elements are: ");        for (int i = 0; i < arr.Length; i++)        {            Console.Write(arr[i] + " ");        }         ArrayList arrList = new ArrayList();         arrList.Add(5);         arrList.Add(1);         arrList.Add(9);         arrList.Add(7);         arrList.Add(4);         Console.Write("\nArrayList elements are: ");         foreach (int i in arrList)         {            Console.Write(i + " ");         }      }   } }

The output of the above program is given as follows:

Array elements are: 4 7 3 9 1 ArrayList elements are: 5 1 9 7 4


Discussion

No Comment Found