Difference between Array, ArrayList and GenericList

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Array:
Basic difference is that arrays are of fixed size. Whereas an ArrayList implements the list data structure and can dynamically grow. While arrays would be more performant that a list, a list would be far more flexible since you don’t need to know the required size initially.

ArrayList:

In case of Arraylist C# compiler do boxing and unboxing of the value to object.

GenericList:
In case of GenericList there is no need to do boxing and unboxing.

Please find the difference below:
1) ArrayList, under the covers this is an Object[], so to put an int value into the ArrayList we’ll have to box it (in the process creating a heap allocated Int32) and then put the reference to the boxed value into the array.
2) List<int>, with the generic implementation under the covers we’re using an int[], no boxing needed and instead we just shove the actual value being added into the array (thereby avoiding creating the heap allocated Int32).


Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.