top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Difference between Generics and Array List?

+8 votes

Which one is used where and how?

posted Feb 6, 2014 by Merry

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
  1. ArrayList belongs to the System.Collections namespace, i.e. you need to import the following namespace.
  2. 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();
arrList.Add("Mudassar Khan");

  1. 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[0]);
string name = arrList[1].ToString();
DateTime dt = Convert.ToDateTime(arrList[2]);

  1. ArrayList is mainly for .Net 2.0 Framework projects as during that period Generic List was not invented.
  2. 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)


  1. Generic List (List) belongs to the System.Collections.Generic namespace, i.e. you need to import the following namespace.
  2. 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();
lstString.Add("Mudassar Khan");
lstString.Add("Robert Hammond");
lstString.Add("Ramesh Singh");

List lstInt = new List();

List lstDateTime = new List();

  1. 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[0];
    string name = lstString[0];
    DateTime dt = lstDateTime[0];

  2. Generic List must be used instead of ArrayList unless specific requirement for projects higher than .Net 2.0 Framework.

  3. 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)
answer Feb 6, 2014 by Atul Mishra
Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.