web training room for online courses
Learn C# programming Language, Free C# Tutorial for Beginners, C# Development Guide

Arrays, ArrayList Example in C#

Array and ArrayList both are used to store collection of object, but there are some different, in real-time application development you come across situation where you need to decide which one to use, both are very useful.

What is an Arrays in C#?
An Array is a strongly-typed collection that store a series of elements same type
int[] myArray=new int[]{5};
                myArray[0] = 1;
                myArray[1] = 2;
                myArray[2] = 3;

As you can see in above int array we could add only integer type data

What is an ArrayList in C#?
An ArrayList is not a strongly-typed collection. It can store the values of different data types or same data type. The size of an array list can increases or decreases dynamically, it can take any size of values from any data type.

ArrayList myArrayList = new ArrayList();
myArrayList.Add(1);
myArrayList.Add("this is string");
myArrayList.Add(1.2);
myArrayList.Add(DateTime.Now);

As you can see in above ArrayList we have added different data types integer, string, decimal, date etc

Difference between Array and ArrayList

Array ArrayList
Arrays belong to System.Array namespace using System; Arraylist belongs to System.Collection namespaces using System.Collections;
In Array, we can store only one datatype either int, char, string etc In Arraylist, we can store different datatype values
Array cant accept null ArrayList collection accepts null

Comment
Name Email Website
Subscribe
 
Learn Array , ArrayList
in C# Tutorial
Learn C# Online Tutorial


Consulting