Difference Between ArrayList and Generic.List C#/VB.Net

Hi,

Last few Days, I learnt a very basic and important thing about  that ,When one should go for ArrayList or Generic.List in coding......On basis of that, I found  few difference between both.


ArrayList

  1. Its comes under System.Collections namespace.
  2. Its contains Array of Object.i.e Any objects type we can store in a ArrayList.We can store any no. of objects at runtime.
  3. ArrayList aryList= new ArrayList(); //No Need to specify the object type,can store anything
  4. arList.Add(1);
  5. arList.Add("Shubhank");
  6. arList.Add("False");
  7. We can store any objects in ArrayList and can retrieve only Objects from ArrayList...
  8. We can say that ArrayList is not Type-Safe.At the Time of inserting recods ,we can store any type of objects,but at the time of fetching we can't be sure that  Values  which are populate by arList (ArrayList object ) of what type..however.. we have to following code while fetching records/items from Array list
  9. foreach(Object o in arList) { ....}
  10. Because of above  ArrayList is not Fast.Need explicit casting while fetching record from it.

Generic.List

  1.   Its come under System.Collections.Generic.
  2. In List object,we can store only Type of object,which is mentioning while initializing List object 
  3. List objList= new List.here we are mentioning string type value i.e in List object only string value can be added,if one has try to insert int type value,then it will through compile time error...
  4. List are Type Safe.
  5. objList.Add("Shubhank");
  6. objList.Add("Upadhyay:);
  7. objList.Add(1);// will give Compile time error
  8. As per declaring of List object we are mentioning string type,So on fetching records from List object we are sure that value are string type.We do not need  to do explicit casting while fetching.
  9. Because of Above Generic.List is Fast.



Regards
Shubhank Upadhyay

Comments

  1. another One point is their regarding boxing and unboxing in arraylist n list...so can u please explain it

    ReplyDelete
  2. http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx

    ReplyDelete
  3. very informative. thanks
    take a look into this http://www.etechpulse.com/2014/05/c-difference-between-arraylist-and-list.html

    ReplyDelete

Post a Comment

Popular posts from this blog

Cloud and How I cleared AZ-900 Microsoft Azure Fundamental exam and Why I choose fundamental

Posting Blog After 10 year :)