ArrayList vs List<object>
Solution 1
You'll be able to use the LINQ extension methods directly with List<object>
, but not with ArrayList
, unless you inject a Cast<object>()
/ OfType<object>
(thanks to IEnumerable<object>
vs IEnumerable
). That's worth quite a bit, even if you don't need type safety etc.
The speed will be about the same; structs will still be boxed, etc - so there isn't much else to tell them apart. Except that I tend to see ArrayList
as "oops, somebody is writing legacy code again..." ;-p
Solution 2
One big benefit to using List<object>
is that these days most code is written to use the generic classes/interfaces. I suspect that these days most people would write a method that takes a IList<object>
instead of an IList
. Since ArrayList
doesn't implement IList<object>
you wouldn't be able to use an array list in these scenarios.
I tend to think of the non-generic classes/interfaces as legacy code and avoid them whenever possible.
Solution 3
In this case, ArrayList
vs. List<Object>
then you won't notice any differences in speed. There might be some differences in the actual methods available on each of these, particular in .NET 3.5 and counting extension methods, but that has more to do with ArrayList being somewhat deprecated than anything else.
Solution 4
Yes, besides being typesafe, generic collections might be actually faster.
From the MSDN (http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx)
The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.
Solution 5
Do some benchmarking and you will know what performs best. I guestimate that the difference is very small.
faulty
I'm a software and hardware engineer. I've move to writing in python coming from .Net and PHP. I also write firmware for PICMicro in ASM and Arduino in C. Occasionally do PCB design and make hardware which interfaces with the PC applications. Recently move into cloud role, focusing on DevSecOps.
Updated on July 26, 2022Comments
-
faulty almost 2 years
I saw this reply from Jon on Initialize generic object with unknown type:
If you want a single collection to contain multiple unrelated types of values, however, you will have to use
List<object>
I'm not comparing
ArrayList
vsList<>
, butArrayList
vsList<object>
, as both will be exposing elements of typeobject
. What would be the benefit of using either one in this case?EDIT: It's no concern for type safety here, since both class is exposing
object
as its item. One still needs to cast fromobject
to the desired type. I'm more interested in anything other than type safety.EDIT: Thanks Marc Gravell and Sean for the answer. Sorry, I can only pick 1 as answer, so I'll up vote both.
-
faulty over 15 yearsObviously I'm not referring to type safety here
-
shen over 13 yearsactually im not sure the question is about performance but that is a good point. my guess is arraylist perf would be worse due to additional boxing, unboxing.
-
Brian over 13 years
List<object>
doesn't give you much in the way of improvements to type safety and performance over using anArrayList
Most of these improvements are specific toList<T>
where T is anything exceptobject
. -
Brian over 13 years@Anonymous: What additional boxing?
ArrayList
andList<object>
should have the same amount of boxing/unboxing. -
shen over 13 years"a single collection to contain multiple unrelated types of values" - my understanding was that when adding different types to a List collection object there was boxing/unboxing occuring, whereas with the arraylist there was not. I guess I am wrong about this.