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
Generic.List
Regards
Shubhank Upadhyay
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
- Its comes under System.Collections namespace.
- 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.
- ArrayList aryList= new ArrayList(); //No Need to specify the object type,can store anything
- arList.Add(1);
- arList.Add("Shubhank");
- arList.Add("False");
- We can store any objects in ArrayList and can retrieve only Objects from ArrayList...
- 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
- foreach(Object o in arList) { ....}
- Because of above ArrayList is not Fast.Need explicit casting while fetching record from it.
Generic.List
- Its come under System.Collections.Generic.
- In List
object,we can store only Type of object,which is mentioning while initializing List object - 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... - List are Type Safe.
- objList.Add("Shubhank");
- objList.Add("Upadhyay:);
- objList.Add(1);// will give Compile time error
- 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.
- Because of Above Generic.List is Fast.
Regards
Shubhank Upadhyay
another One point is their regarding boxing and unboxing in arraylist n list...so can u please explain it
ReplyDeletehttp://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
ReplyDeletevery informative. thanks
ReplyDeletetake a look into this http://www.etechpulse.com/2014/05/c-difference-between-arraylist-and-list.html