Difference between Generic List and Array List in C#?

 Posted by Senthilstayss on 3/16/2013 | Category: C# Interview questions | Views: 55068 | Points: 40

1. ArrayList and Generic List are used to store any type of objects.Consider that you are intended to store only the integer values.In this case when we use ArrayList , we can store any type of objects(i.e., Integer,String or some custom objects).

ArrayList objarList=new ArrayList();
objarList.add("senthil");// will compile..but will throw run time error.

It won't throw any compile time error.It will throw only run time error when we iterate values in the array list by using for each.

But when we use the Generic List ,we can restrict in the compile time itself.If you are intended to store only integer values,then it will allow to store only integer values.If you try to store any other value other than integer, then it will throw compile time error.

List<int> objList=new List<int>();
objList.add("senthil") // Will result in compile time error.

2.Whenever you are adding the objects in the ArrayList boxing will happen at that time.Whenever you are accessing the objects from the ArrayList unboxing will happen at that time.So it will hurt the performance. But in case of Generic List there will not be any Boxing or UnBoxing problem and also it will provide the better type safety.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Sarangrao on: 3/5/2014 | Points: 10
If we iterate in the ArrayList by using foreach,
whether it contains integer or string....It will NOT show any runtime error

Login to post response