Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't ArrayList marked [Obsolete]?

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.

like image 481
Cheng Chen Avatar asked Feb 21 '11 06:02

Cheng Chen


People also ask

What is the problem with ArrayList?

The main problem with ArrayList is that is uses object - it means you have to cast to and from whatever you are encapsulating.

Is ArrayList deprecated?

It is not deprecated (yet!) but as stated remarks section on it's documentation, it is not recommended to use it by Microsoft itself. As others stated, ArrayList is a hold-over from a time when C# did not have generics. ArrayList gave developers a way to generate lists (castable at run-time) of objects.


2 Answers

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.

like image 103
Jon Skeet Avatar answered Sep 18 '22 17:09

Jon Skeet


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.

like image 26
Marc Gravell Avatar answered Sep 16 '22 17:09

Marc Gravell