views:

304

answers:

6

Recently I asked a question on SO that had mentioned the possible use of an c# ArrayList for a solution. A comment was made that using an arraylist is bad. I would like to more about this. I have never heard this statement before about arraylists. could sombody bring me up to speed on the possible performance problems with using arraylists

c#. .net-2

+19  A: 

The main problem with ArrayList is that is uses object - it means you have to cast to and from whatever you are encapsulating. It is a remnant of the days before generics and is probably around for backwards compatibility only.

You do not have the type safety with ArrayList that you have with a generic list. The performance issue is in the need to cast objects back to the original (or have implicit boxing happen).

Implicit boxing will happen whenever you use a value type - it will be boxed when put into the ArrayList and unboxed when referenced.

The issue is not just that of performance, but also of readablity and correctness. Since generics came in, this object has become obsolete and would only be needed in .NET 1.0/1.1 code.

Oded
Type Safety too
Russ Cam
@Russ Cam - thanks for the comment. Answer updated as you were commenting ;)
Oded
+1 for covering all the bases.
tster
+1  A: 

In addition to the performance issues, it's a matter of moving errors from runtime to compile time. Casting objects retrieved from ArrayLists must happen at runtime, and any type errors will happen during execution. Using a generic List<> all types are checked during compile time.

recursive
It's a performance question, as well...
Reed Copsey
big time performance improvements using List<int> instead of ArrayList with ints in it.
tster
This is not a performance question. Yet, performance issues is part of the answer.
AMissico
From the OP: "could sombody bring me up to speed on the possible performance problems with using arraylists"
tster
@tster: I saw the post in question, and the primary objections did not seem to be performance related. Nonetheless, I've changed the wording.
recursive
Perhaps “Performance” is not the correct term to use, English not being my first language ,I don't always think of the term “performance” as a speed related dimension.
fishhead
+3  A: 

The generic List<T> is preferred since it is generic, which provides additional type information and removes the need to box/unbox value types added to it.

Lee
@DOK - `List<T>` exists in .net 2.0
Lee
@DOK, .NET 2.0 has generics.
Cylon Cat
Is there any generic equivalent to the vb.net Collection type, which allows an iterator to delete the current item (assuming the item's content contains information identifying the key)? Dictionary would be a good match, except for the last point.
supercat
+2  A: 

ArrayList is not a generic type so it must store all items you place in it as objects. This is bad for two reasons. First, when putting value types in the ArrayList you force the compiler to box the value type into a reference type which can be costly. Second, you now have to cast everything you pull out of the array list. This is bad since you now need to make sure you know what objects are in there.

List avoids these issues since it is constructed with the proper type. For example:

List<int> ints =  new List<int>();
ints.Add(5); //no boxing
int num = ints[0]; // no casting
Matthew Manela
+9  A: 

If you're storing a value type (int, float, double, etc - or any struct), ArrayList will cause boxing on every storage and unboxing on every element access. This can be a significant hit to performance.

In addition, there is a complete lack of type safety with ArrayList. Since everything is stored as "object", there's an extra burden on you, as a developer, to keep it safe.

In addition, if you want the behavior of storing objects, you can always use List<object>. There is no disadvantage to this over ArrayList, and it has one large (IMO) advantage: It makes your intent (storing an untyped object) clear from the start.

ArrayList really only exists, and should only be used, for .NET 1.1 code. There really is no reason to use it in .NET 2+.

Reed Copsey
+1  A: 

All the boxing and unboxing can be expensive and fragile. Microsoft made some nice improvments in terms of typing and performance in .NET 2.0 generics.

Here are some good reads:

Christopher Painter