ArrayList Class Vs. List Generic Class

G

Guest

Hello,

I need an array of objects. I am considering using either ArrayList Class
or List Generic Class.

Any guidelins regarding when to use one or the other?

Thanks
Eitan
 
N

Nicholas Paldino [.NET/C# MVP]

Eitan,

The only reason I can see using an ArrayList is when all the values that
you are going to store do not have a common base class (other than object).
However, in this case, I would suggest using List<object>, as List<object>
supports the IList<T> interface still.

If the types that you are going to store in the list are all of the same
type, then use the generic class. In most instances (especially when
storing value types, because there is no boxing) you will see a performance
gain over the ArrayList class (even with reference types, there is a slight
performance gain).
 
D

Daniel Cigic

ArrayList have some *build-in* methods/properties like SyncRoot or
Synchronized(ArrayList/IList) that can be
helpful for the thread sync. if needed. I don't think that generic List have
that.
 
J

Jon Skeet [C# MVP]

Daniel Cigic said:
ArrayList have some *build-in* methods/properties like SyncRoot or
Synchronized(ArrayList/IList) that can be
helpful for the thread sync. if needed. I don't think that generic List have
that.

No, although there's SynchronizedCollection<T>.

Personally I dislike using the "synchronized" version of ArrayList as
it gives a false impression of automatic thread safety. You still need
to manually lock when you need to do anything which comprises more than
one operation, eg iterating through a list.
 

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