What is the difference between Array and Arraylist?

 Posted by BABU_AKKANDI on 4/6/2009 | Category: .NET Framework Interview questions | Views: 16464

As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Nil1401 on: 3/6/2012 | Points: 10
An Array is a high performance way of storing a group of data with the same type because each element is laid out next to it's neighbor in memory. This allows for very fast access because:
(a) the code can do a little math and jump quickly to any location in the array, and
(b) the elements are all grouped together so they tend to be in memory at the same time (fewer page faults and cache misses).
An array in .NET is actually a class (System.Array), but a special type of class that is well understood by the .NET engine (CLR). Because of this, you can standard array access notation (text languages) such as foo[3] = 99;

In ArrayList, however, you are dealing with a collection. There are several types of collections in .NET (see the System.Collections and System.Collections.Specialized namespaces), but the key thing about them is the interfaces they support (IEnumerable, ICollections, IList, etc). If you look at the definition of these interfaces, you see that collections are all about grouping things together and providing methods to access them.With an ArrayList, however, if you add one element more than the internal array can handle, the ArrayList automatically creates a larger array and copies the old array into the new array.

Login to post response