ArrayList vs. List<>

Z

Zytan

The docs for List say "The List class is the generic equivalent of the
ArrayList class." Since List<> is strongly typed, and ArrayList has
no type (is that called weakly typed?), I would assume List<> is far
better. So, why do people use ArrayList so often? Am I missing
somehing? What's the difference between them?

Zytan
 
J

Jon Skeet [C# MVP]

Zytan said:
The docs for List say "The List class is the generic equivalent of the
ArrayList class." Since List<> is strongly typed, and ArrayList has
no type (is that called weakly typed?), I would assume List<> is far
better. So, why do people use ArrayList so often? Am I missing
somehing? What's the difference between them?

ArrayList was available from the start - List<T> only arrived in .NET
2.0, along with generics.

There are a lot of old samples, and samples which want to stay version-
neutral.
 
A

Alvin Bruney [MVP]

There is a whole lot of performance difference between the two in favor of
lists. Mostly, people use arraylist because of habit and it is functionally
more familiar than lists are. Eventually, that should change though.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
 
C

Crash

The docs for List say "The List class is the generic equivalent of the
ArrayList class." Since List<> is strongly typed, and ArrayList has
no type (is that called weakly typed?), I would assume List<> is far
better. So, why do people use ArrayList so often? Am I missing
somehing? What's the difference between them?

Zytan

Version 1.x of the .NET framework did not support generics so you had
no option other than to use the non-type safe ArrayList. Generics are
generally preferred as they promote type safetly and also because they
eliminate the need to box/unbox collections of value type...
 
Z

Zytan

ArrayList was available from the start - List said:
2.0, along with generics.

There are a lot of old samples, and samples which want to stay version-
neutral.

Yes, tell me about it. But I supposed that's due to the quick
popularity of the language, so I can't be mad at that.

So, you are saying that List<T> is indeed better? Was I right about
that?

Zytan
 
Z

Zytan

There is a whole lot of performance difference between the two in favor of
lists. Mostly, people use arraylist because of habit and it is functionally
more familiar than lists are. Eventually, that should change though.

Alvin, thanks, so List<T> is faster, type safe (strongly typed), and
more easy to use, so, hm, which should I use? haha. thanks

Zytan
 
Z

Zytan

Version 1.x of the .NET framework did not support generics so you had
no option other than to use the non-type safe ArrayList. Generics are
generally preferred as they promote type safetly and also because they
eliminate the need to box/unbox collections of value type...

Ok, thanks for the reply!

Zytan
 
P

Peter Duniho

The docs for List say "The List class is the generic equivalent of the
ArrayList class." Since List<> is strongly typed, and ArrayList has
no type (is that called weakly typed?), I would assume List<> is far
better. So, why do people use ArrayList so often? Am I missing
somehing? What's the difference between them?

The difference? The List class is the generic equivalent of the ArrayList
class. :)

The only reason I can think of off the top of my head to use the ArrayList
class is when you need to pass one to an existing .NET method. Otherwise,
I'd use the List<> class (or even its relatives, such as LinkedList<>).

Have I mentioned lately how much I love generics in C#? :)

Pete
 
C

Carl Daniel [VC++ MVP]

Peter said:
Have I mentioned lately how much I love generics in C#? :)

.... just don't try to do generic math in them. :) (Yes, there are ways,
but it's a pain).

-cd
 
J

Jon Harrop

Zytan said:
is that called weakly typed?

No, that is called "dynamically" typed, meaning type checks are deferred
until run-time, slowing down both development and execution. The antonym
is "statically typed", meaning the types are checked at compile type and
run-time type checks are removed.

Python, Ruby, Perl, Lisp and Scheme are dynamically typed languages. C, C++,
C#, Java (mainly), F#, OCaml and Haskell are statically typed languages.
The last three use state-of-the-art techniques to combine the brevity of
dynamic typing with the performance of static typing.

Strong and weak typing refers to the ability to misinterpret a value as
being of another type. For example, you can accidentally get the 64-bit int
representing the bits of a 64-bit float in the C language. Modern languages
almost always try to be strongly typed to avoid such errors. Then you must
explicitly invoke a function to get at the bits of a floating point number.
 
M

Marc Scheuner

The only reason I can think of off the top of my head to use the ArrayList
class is when you need to pass one to an existing .NET method.

Or if you need to have a list of objects which are not all of the same
type - that won't jive in a List<T> :)

Marc
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Marc said:
Or if you need to have a list of objects which are not all of the same
type - that won't jive in a List<T> :)

Marc

Oh, but it will. Just make a List<object>. :)
 
C

Cor Ligthert [MVP]

"> Alvin, thanks, so List<T> is faster,

No it is in fact slower, a strongly typed type has always extra code.
However it is type safe and helps you to make more secure programs.

If you use the arraylist only inside a method with one attribute and let it
go out of scoop afterwards, the arraylist is probably the fastest.

Cor
 
J

Jon Skeet [C# MVP]

Zytan said:
Yes, tell me about it. But I supposed that's due to the quick
popularity of the language, so I can't be mad at that.

So, you are saying that List<T> is indeed better? Was I right about
that?

Yup. I can't think of any reason - off the top of my head, anyway - to
use ArrayList rather than List<T> if you know you *can* use List<T> in
all target environments.
 
J

Jon Skeet [C# MVP]

Jon Harrop said:
No, that is called "dynamically" typed, meaning type checks are deferred
until run-time, slowing down both development and execution. The antonym
is "statically typed", meaning the types are checked at compile type and
run-time type checks are removed.

Python, Ruby, Perl, Lisp and Scheme are dynamically typed languages. C, C++,
C#, Java (mainly), F#, OCaml and Haskell are statically typed languages.
The last three use state-of-the-art techniques to combine the brevity of
dynamic typing with the performance of static typing.

Strong and weak typing refers to the ability to misinterpret a value as
being of another type. For example, you can accidentally get the 64-bit int
representing the bits of a 64-bit float in the C language. Modern languages
almost always try to be strongly typed to avoid such errors. Then you must
explicitly invoke a function to get at the bits of a floating point number.

I've been looking into this recently, and the latter point is usually
called safe or unsafe typing, IMO. Strong vs weak typing is very poorly
defined - different people use it to mean all kinds of different
things.

A few useful links to read up on:
http://en.wikipedia.org/wiki/Strong_typing
http://en.wikipedia.org/wiki/Type_system
http://eli.thegreenplace.net/2006/11/25/a-taxonomy-of-typing-systems/
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
"> Alvin, thanks, so List<T> is faster,

No it is in fact slower, a strongly typed type has always extra code.
However it is type safe and helps you to make more secure programs.

It entirely depends on what you're storing in the ArrayList. If you're
storing value types, that will be slower and more memory hungry because
it will be boxing for every store and unboxing on every fetch.

For reference types, I would still expect it to be faster to use a
List<T> because of the lack of need for a runtime cast while fetching,
but I haven't tried it. I'd expect the difference in this case either
way to be smaller than the difference between using value types in an
arraylist and in a List<T>.
 
C

Christof Nordiek

Cor Ligthert said:
"> Alvin, thanks, so List<T> is faster,

No it is in fact slower, a strongly typed type has always extra code.

Do you really think?
I couldn't imagine any reason why generic lists should be slower then
ArrayList.
Can you demonstrate this with some sample code?

I don't know where there should be extra code. Only that each method is
compiled (from IL to native) ones for each value type, it is used with and
once for all reference types (IIRC). But the use with value types surely is
faster with List<> than with ArrayList because of boxing and unboxing.

Christof
 
B

Ben Voigt

Marc Gravell said:
Maybe for 1.1-style typed collections etc... but for generics?

Compile-time enforcement (ala generics) is by nature cheaper than run-time
enforcement. And just try using ArrayList without some sort of runtime
enforcement (you do need to cast back from object eventually, unless it's a
list of hashtable keys or some equally bizarre circumstance).

That said, the CLR with JIT blurs the line between compile-time and
run-time. All bets are off concerning which IL will verify faster. So if
it's only run once, it's anybody's guess which is faster. But if it only
runs once, why is anyone concerned about performance. If you have a loop,
the generic wins by a huge margin.
 
Z

Zytan

Have I mentioned lately how much I love generics in C#? :)

You're not the only one! :)

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top