Difference between ArrayList and List<object> ?

  • Thread starter Thread starter Murat Ozgur
  • Start date Start date
M

Murat Ozgur

Hello,

Is there any difference between ArrayList and List<object> ? Which
one should I use ?

Thanks.
 
Is there any difference between ArrayList and List<object> ? Which
one should I use ?

I think List< object > should be better, because it uses generics; so
it is type safer than ArrayList (in which you can put everything
derived from base object class).
 
Not much difference really, I'd probably use List<Object> because then
it's clear to other people that your intention was to store a list of
Objects (or various derivative types), rather than a list of some other
specific type that would always need explicitly casting.
 
Is there any difference between ArrayList and List said:
one should I use ?

I'd choose to use List<object> because of the improved APIs on List<object>
that aren't available on ArrayList.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Murat,

I like the extended API of List<T>. Also, I wouldn't be surprised if
they use different growth algorithms.

Brian
 
* Murat Ozgur said:
Hello,

Is there any difference between ArrayList and List<object> ? Which
one should I use ?

List<object> is type safe and more efficient since it doesn't require
boxing operations (converting a value type to a reference type, and vice
versa) so it could be more efficient depending on what you store in your
list.
 
Angel Of Death said:
List<object> is type safe and more efficient since it doesn't require
boxing operations (converting a value type to a reference type, and
vice
versa) so it could be more efficient depending on what you store in
your
list.

Claiming that "List<object> is type safe" is sort of silly.
Since it has a type of object that kind of wipes out type saftey since
everything is an object.

A better overall suggestion would be to use List<SomeType>. Then you can
talk about type safe.

Bill
 
List<object> is type safe

Of course it isn't!

List<string> is type-safe, List<SqlParameter> is type-safe, but List<object>
*clearly* isn't... :-)

List<object> objList = new List<object>;
objList.Add(new string);
objList.Add(new int);

etc
 
MrAsm said:
I think List< object > should be better, because it uses generics; so
it is type safer than ArrayList (in which you can put everything
derived from base object class).

No, there is no difference in type safety. A reference to an object is
not more type safe just because you use generics.
 
Göran Andersson said:
No, there is no difference in type safety. A reference to an object is not
more type safe just because you use generics.

I think what he means is that an ArrayList can store multiple types, and
there's no checking to ensure that every element of the array is the same
type as every other element.

Perhaps a more significant benefit would be "type convenient". :)

As you note, C# is type safe no matter how you store a reference. But one
nice thing about the generic types is that because the type is explicitly
stated with the collection, you don't need to cast things as they are
referenced from the collection.

Pete
 
Peter said:
I think what he means is that an ArrayList can store multiple types, and
there's no checking to ensure that every element of the array is the same
type as every other element.

The exact same is true for List said:
Perhaps a more significant benefit would be "type convenient". :)

As you note, C# is type safe no matter how you store a reference. But one
nice thing about the generic types is that because the type is explicitly
stated with the collection, you don't need to cast things as they are
referenced from the collection.

Yes, that is true for a list of a specific type, like List<int>, but not
for List<object>.
 
Please use cross posting instead of doing separate postings of the same
question in a lot of newsgroups.
 

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

Similar Threads


Back
Top