17

Possible Duplicate:
When to use ArrayList over array[] in c#?

From the perspective of memory or processor costs, does there appear to be a significant difference between an array and an arrayList object?

Community
  • 1
  • 1
Mike Olson
  • 191
  • 1
  • 1
  • 7
  • @NikhilAgrawal not a duplicate of that topic since the question is specifically about performance differences, not usage context. – Asik May 25 '12 at 01:42
  • I suspect ArrayList would be slower as you have to cast each element in the array when retrieving. – Matthew May 25 '12 at 02:00

2 Answers2

7

An array is a low-level data structure that essentially maps to a region in memory. An ArrayList is a variable length list implemented as an array of object that is re-allocated as the list grows.

ArrayList therefore has some overhead related to managing the size of the internal array, and more overhead related to casting objects to the correct type when you access the list.

Also, storing everything as object means that value types get boxed on write and unboxed on read, which is extremely detrimental to performance. Using List<T>, a similar but strongly-typed variable size list avoids this issue.

In fact, ArrayList is practically deprecated in favor of List<T> since .NET 2.0.

Asik
  • 21,506
  • 6
  • 72
  • 131
  • There is another object called a simple List. How does it compare with an ArrayList. Which appears to be a more sophisticated control. – Mike Olson May 25 '12 at 01:52
  • @MikeOlson I explained that in my answer already... did you read it? – Asik May 25 '12 at 01:57
  • Sorry yes I read it but wasn't sure if the List was the same as the simple list. So basically this simple list has basically replaced the Array list in favor of ease of use? – Mike Olson May 25 '12 at 02:04
  • @MikeOlson Yes List replaces ArrayList because it is strongly typed which avoids the inconvenience, potential bugs and performance concerns of having to cast to object and back. – Asik May 25 '12 at 02:21
  • Thanks that makes sense. Appreciate your time to answer. – Mike Olson May 25 '12 at 02:30
  • @MikeOlson Then please vote my answer up :) – Asik May 25 '12 at 02:58
  • Overall List is way to go , but ArrayList is used a lot in many products like SharePoint and CRM etc.. – Tom Stickel Mar 05 '14 at 19:34
1

An array is a contiguous block of memory of fixed size, whereas an ArrayList (though you should prefer List since .NET 2.0) wraps an array to provide dynamically-resizable storage.

The "difference" between them being that, as far as they're encapsulated, an ArrayList is resizable, an array isn't. As far as the implementation is concerned: because an ArrayList wraps (and reallocates) arrays it will require more slightly more memory than an array (as it has to know the current number of elements, as opposed to its capacity), furthermore an ArrayList also requires CPU time to reallocate and copy its internal array if it ever reaches its internal capacity.

However, instantiating an ArrayList is no more expensive than allocating an array. The only difference there being the handful of instructions needed to initialize the ArrayList's state. The difference is negligible and not worth worrying about.

You'll find that if you are reallocating an array by yourself as the means of creating a resizable collection then you're better off using ArrayList/List as it has been thoroughly tested.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • 1
    There is another object called a simple List. How does it compare with an ArrayList. Which appears to be a more sophisticated control. – Mike Olson May 25 '12 at 01:51
  • The main difference between ArrayList and Generic List is that generic list is type safe while arraylist is not. – DinoMyte May 18 '14 at 22:10