[ACCEPTED]-ArrayList vs List<object>-arraylist
You'll be able to use the LINQ extension 8 methods directly with List<object>
, but not with ArrayList
, unless 7 you inject a Cast<object>()
/ OfType<object>
(thanks to IEnumerable<object>
vs IEnumerable
). That's 6 worth quite a bit, even if you don't need 5 type safety etc.
The speed will be about 4 the same; structs will still be boxed, etc 3 - so there isn't much else to tell them 2 apart. Except that I tend to see ArrayList
as "oops, somebody 1 is writing legacy code again..." ;-p
One big benefit to using List<object>
is that these 8 days most code is written to use the generic 7 classes/interfaces. I suspect that these 6 days most people would write a method that 5 takes a IList<object>
instead of an IList
. Since ArrayList
doesn't 4 implement IList<object>
you wouldn't be able to use an 3 array list in these scenarios.
I tend to 2 think of the non-generic classes/interfaces 1 as legacy code and avoid them whenever possible.
In this case, ArrayList
vs. List<Object>
then you won't notice 6 any differences in speed. There might be 5 some differences in the actual methods available 4 on each of these, particular in .NET 3.5 3 and counting extension methods, but that 2 has more to do with ArrayList being somewhat 1 deprecated than anything else.
Yes, besides being typesafe, generic collections 6 might be actually faster.
From the MSDN (http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx)
The 5 System.Collections.Generic namespace contains interfaces and classes 4 that define generic collections, which 3 allow users to create strongly typed collections 2 that provide better type safety and performance 1 than non-generic strongly typed collections.
Do some benchmarking and you will know what 2 performs best. I guestimate that the difference 1 is very small.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.