Difference between ArrayList and Array in C#

 

Both ArrayList and Arrays are used to store list of values in C#., but there are some difference between both. In this article we discuss about what are the differences between Array and ArrayList.

 

  • Arrays are strongly coupled typed whereas ArrayLists are loosely coupled types. That means we can store only one type of data in Arrays and in ArrayList we can store mixed type of data

 

  • Arrays have fixed length and we can declare arrays as shown below.

 

                    string[] sNames = new string[10];

 

        Arrays are accessible by index and in general index starts with 0(zero).

 

        Whereas ArrayList is generated from System.Collection namespace. ArrayLists are not fixed length type and we can declare ArrayList as shown below.

 

            ArrayList empInfo = new ArrayList();

            empInfo.Add("John");

            empInfo.Add(1);

            empInfo.Add(10000.23);

 

  • We can access Array as shown below by using index.

 

            string[] sNames = new string[10];

            Console.WriteLine(sNames[0]);

                  (OR)

            foreach(string name in sNames)

                Console.WriteLine(name);

 

        We can access ArrayList as shown below.

 

            ArrayList empInfo = new ArrayList();

            empInfo.Add("John");

            empInfo.Add(1);

            empInfo.Add(10000.23);


            foreach (string name in empInfo)

                Console.WriteLine(name.ToString());

 

  • Elements in Array have to be of same data type whereas elements in ArrayList can be different data type.
  • ArrayList can be resied dynamically whereas Arrays cannot.