- ArrayList belongs to the System.Collections namespace, i.e. you need to import the following namespace.
- ArrayList does not have type restriction for storing data i.e. it is not Type Safe. You can store anything in ArrayList. In fact same ArrayList can store multiple types of objects.
ArrayList arrList = new ArrayList();
- ArrayList stores all data as object thus to get it back you must remember what you stored where and correspondingly Type Cast it as per its original Type when stored.
int number = Convert.ToInt32(arrList);
string name = arrList.ToString();
DateTime dt = Convert.ToDateTime(arrList);
- ArrayList is mainly for .Net 2.0 Framework projects as during that period Generic List was not invented.
- While running a Loop on ArrayList you need to use Object data type. Thus this is another disadvantage as you again do not know what type of data particular item contains.
foreach (object o in arrList)
- Generic List (List) belongs to the System.Collections.Generic namespace, i.e. you need to import the following namespace.
- In Generic List (List), T means data type, i.e. string, int, DateTime, etc. Thus it will store only specific types of objects based on what data type has been specified while declarations i.e. it is Type Safe. Thus if you have a Generic List of string you can only store string values, anything else will give compilation error.
Below I had no option other than having three different Generic Lists for three different data types.
List lstString = new List();
List lstInt = new List();
List lstDateTime = new List();
Generic List stores all data of the data type it is declared thus to getting the data back is hassle free and no type conversions required.
int number = lstInt;
string name = lstString;
DateTime dt = lstDateTime;
Generic List must be used instead of ArrayList unless specific requirement for projects higher than .Net 2.0 Framework.
- While running a Loop on Generic List again it is problem free as we exactly know what the List contains.
foreach (int number in lstInt)
foreach (string name in lstString)
foreach (DateTime dt in lstDateTime)