39

After a deep thought and looking into the implementation of ArrayList, personally I really want to say It's obsolete, I have no reason to use this class after 2.0. But since it's not marked as [Obsolete], is there any usage that I didn't know, better than using a generic class? If yes, please give an example. Thanks.

EDIT Let's take List<T> as an example, it provides all functions of ArrayList, and it's strongly-typed. So when do we need to use ArrayList? Maybe sometimes it has a better performance? I don't know. I appreciate if you can show me something special of ArrayList.

Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • 1
    @glowcoder: Comparing it with the generics. – Cheng Chen Feb 21 '11 at 07:00
  • 2
    I think you've already decided generics have made it *functionally* obsolete. So this question is really "Why hasn't the .NET team marked it with the `[Obsolete]` attribute yet?" There is a pretty limited number of people that can answer *that* question. – Cody Gray - on strike Feb 21 '11 at 07:06
  • 2
    Right offhand, List doesn't implement ICloneable like ArrayList does. – corsiKa Feb 21 '11 at 07:07
  • 2
    @Cody Gray: I just want to make sure generics is *always* better than ArrayList, in case of something I didn't know... – Cheng Chen Feb 21 '11 at 07:08
  • There are some tricks you can do with ArrayList you can't with a List... There is the ArrayList.Adapter for example (you build an ArrayList object that points to an IList object) – xanatos Feb 21 '11 at 07:12
  • 2
    @xanatos: It's a circle again. If I won't use ArrayList any more, why do I need to convert an IList to ArrayList? – Cheng Chen Feb 21 '11 at 07:17
  • 1
    @Danny Some legacy interfaces could return an IList. You question was about obsoleting ArrayList, it wasn't about obsoleting the whole "non-generic" collections. In a perfect world, where everyone is already working on .NET 4.0, I think they should do it... But... Wait... This isn't a perfect world!! Many companies are still working on 1.1!! :-) (I mean that unless it's broken, sometimes it's better to not remove it. Programmers have better things to do than change ArrayLists to List, perhaps not even knowing what they are touching) – xanatos Feb 21 '11 at 07:24
  • @xanatos: That last comment makes a lot more sense than your answer. Consider adding it to your answer so that people don't have to read all the comments in order to see it. – Cody Gray - on strike Feb 21 '11 at 08:25
  • The last time I had to use `ArrayList` was for school (last year)... they were crazy about it and I swear it's got something to do with Java's `ArrayList`. – BoltClock Feb 21 '11 at 11:51
  • @BoltClock Well, Java's `java.util.ArrayList` is actually generic (at least as of Java 5). – corsiKa Feb 21 '11 at 15:00
  • @glowcoder: I only recall using the non-generic version... guess my school isn't a big fan of generics. – BoltClock Feb 21 '11 at 15:01
  • @BoltClock well, java5 has been out since 2004, so I would hope it's not that they just haven't upgraded their lab machine jdks yet... Your profs probably just never updated their lesson plans or something. – corsiKa Feb 21 '11 at 15:10

3 Answers3

49

I think it should be considered effectively obsolete for new code, but there's no compelling reason to mark it obsolete and create warnings in all code which was written before 2.0 was released.

In my experience, most of the types and members which have been marked obsolete by Microsoft are actively dangerous in some respect, and should really be fixed if you still have a codebase using them. While using ArrayList is painful and (at least theoretically) prone to discovering type-related bugs at execution time rather than compile time, the type does its job well enough... often there's really no compelling reason to change existing code. It's the kind of change I'd generally consider when I already happened to be working on an area of code which was using ArrayList, rather than actively seeking out every usage of it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • +1 because it's a very good response. Some examples are Thread.Suspend, Thread.Resume. – xanatos Feb 21 '11 at 07:29
  • 7
    I wish the .net compilers had a 'warning for legacy framework elements' flag. I.e. elements which would disappear if the framework 4 and associated languages were recreated from scratch today. – vc 74 Feb 21 '11 at 08:42
  • Not everyone can throw a Python :-) (a reference to Python 3.0 that "cleaned up the mess" and is partially incompatible with Python 2.0)... Or... not everyone can re-throw a VB.NET (a reference to VB.NET that was incompatible with VB 6.0) – xanatos Feb 21 '11 at 15:09
  • 1
    @vc 74: Something like [Obsolete, from=4.0] would be great. If the project targets 2.0 everything's fine, if target is 4.0 warnings are given. – Mene Feb 24 '11 at 00:13
  • 2
    @Mene: yes, I'd be happy too with a more restrictive option like [EnforceBackwardCompatibility=false]. Which could be a set of fxcop rules that MS could ship. – vc 74 Feb 24 '11 at 08:21
15

Actually it is completely removed from Silverlight - so the intention is there. Presumably there is simply too much old existing code for regular .NET that uses ArrayList to obsolete it, especially since a lot of people run with warnings-as-errors.

You shouldn't use it in new code without good reason.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • +1 for the "warnings as errors". Eric Lippert mentioned a couple of times that they do plan for users that use that switch – Michael Stum Feb 22 '11 at 23:28
  • "plan for users that use that switch" -- that should be all users. As a matter of fact, there should be no such switch, it should be the default behavior and the only behavior. Nobody should be using binaries produced by a build that yielded warnings. – Mike Nakis Oct 13 '22 at 11:39
  • The difference between errors and warnings is widely understood as being that with an error, you have to fix it before you can proceed, whereas with a warning, you can just ignore it and proceed. That's wrong. The difference should be that you can suppress a warning whereas you cannot suppress an error. But all n00b programmers out there should be forced deal with all warnings, either by resolving them or by consciously and explicitly suppressing them, before being allowed to proceed. – Mike Nakis Oct 13 '22 at 11:40
5

It isn't "obsolete" per se. It's "obsolete" as a '70 '80 early '90 car. If I had to choose between a List<Object> and an ArrayList there is a VERY VERY SMALL possibility I would use an ArrayList... Forget it... It doesn't implement IEnumerable<Object>, so to use Linq I would have to use a OfType<Object>().

To make an example:

var aaa = new ArrayList();
var aaaa = aaa.OfType<object>().Where(p => p != null);

var bbb = new List<object>;
var bbbb = bbb.Where(p => p != null);

Someone finally upvoted my response, so I'll add something to it :-)

Had you asked "would you use any non-generic collection" my response would have been different. The Hashtable has an interesting property:

Hashtable is thread safe for use by multiple reader threads and a single writing thread. It is thread safe for multi-thread use when only one of the threads perform write (update) operations, which allows for lock-free reads provided that the writers are serialized to the Hashtable.

So there are places where an Hashtable should be better than a lock + Dictionary or a ConcurrentDictionary (but you would have to benchmark it)

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • 9
    Your answer reads like a circle. What point are you trying to make? Is it obsolete or not? – Cody Gray - on strike Feb 21 '11 at 07:07
  • 1
    As a '70 car. If you could choose between a '70 car and a 2010 car, at the same price, what would you buy? I was ironic (the "forget it" part) – xanatos Feb 21 '11 at 07:10
  • 5
    @xanatos: An antique collector would choose a '70 car. – Cheng Chen Feb 21 '11 at 07:12
  • 4
    @Danny - Maybe a collector of antique code would collect COBOL and ArrayLists. – Greg Feb 21 '11 at 07:13
  • 1
    @Danny So you can use your .NET 1.1 and live happy :-) '70 cars are STILL street legal, but 90% of the persons would buy the 2010 car, that *normally* is better. You see, my metaphor was better than I thought :-) :-) – xanatos Feb 21 '11 at 07:14
  • But doesn't specifying `object` as a generic type parameter totally defeat the point of using generics in the first place? If I genuinely had to hold a heterogenous collection of objects with no specific type, I think `ArrayList` is the more logical choice. It's not like you can really achieve anything non-trivial using LINQ on such a non-specific collection anyway. – Bradley Smith Feb 21 '11 at 07:43
  • I could create a convolute `.Select(p => p is Something ? 1 : p is SomethingElse ? 2 : 0)`. The problem isn't ArrayList. It's all the non-generic collections. You can't Osbolete ArrayList without Obsoleting everything else. They are the "old-non-strong-typed way of life". – xanatos Feb 21 '11 at 07:47
  • The metaphor is flawed, it gives the impression that `ArrayList` is some kind of luxury thing that only true adepts of programming know how to use and love whereas in reality it's just an ugly error-prone non-generic collection that shouldn't be used – Dyppl Feb 21 '11 at 09:02
  • I didn't mean a "collector" car. I meant a "run of the mill" '70 car and a "run of the mill" 2010 car. So we are speaking of an obsolete-no-airbags-no-abs-no-esp against a newer yes-airbags-yes-abs-probably-yes-esp – xanatos Feb 21 '11 at 09:07
  • @xanatos By virtue of still being around in drivable condition, any '70s car is now a collector car. Similarly, we all work for places that have code that dates back to (company founding/IT department founding/other defining moment) that, because it is still running, we're not getting rid of it, despite the fact we could rewrite it in a modern language in 9-12 months... – corsiKa Feb 21 '11 at 15:04
  • 1
    I doubt 10% of all drivers are antique car collectors ;) – Joseph Yaduvanshi Feb 21 '11 at 15:44
  • '80... Is it ok if I use '80? The AVERAGE age of a passenger vehicle is like 9.4 years (wiki). So a 30 years old car is only OLD. (technically here in italy a 20 years old car could be "storically important", but it's only for some models). Someone is ruining a marvelous metaphor nitpicking it in the details. This is a sad sad world. :-) And no one voted up my response. I think life can be cruel... :-) – xanatos Feb 21 '11 at 16:03