Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     

C#/.NET interview Question - How will you distinguish between ArrayList and Array?

They are fixed length.
They are resizable and variable length.
They are compiled strong type collection.
They are flexible and can accommodate any data types.
Because arrays are of fixed size and strong type collection performance is faster.
In arraylist lots of boxing and unboxing are done there for its performance is slower.

Let’s see an example to prove the above differences.

The below is how we declare Array.
String [ ] str = new String [5]; // here you see that the length is fixed as [5] 
and the data type is also defined as string.

Now, see how you can declare ArrayList.
ArrayList   str = new ArrayList (); // here you see that the length is not fixed and 
is not tied up with a data type.
If you go to add data to an ArrayList you can see it takes object which is a very generic type. Due to this boxing, unboxing or casting are done. In other words data moves from stack to heap or heap to stack. If you look at Array as they are strong types boxing and unboxing are completely avoided. Therefore, Array performance is faster as compared to ArrayList. 

The below diagram gives a better idea of the differences between Array and ArrayList.


Please click here to see more C#/.NET interview questions


Visit Authors blog for more  C#/.NET interview questions

Share this article   |    Print    |    Article read by 5049 times
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Related Articles:
Related Interview Questions: