为什么 ArrayList 没有标记为 [Obsolete]?
经过深思熟虑并研究 ArrayList
,我个人很想说它已经过时了,我没有理由在2.0之后使用这个类。但由于它没有标记为[Obsolete]
,是否有任何我不知道的用法,比使用泛型类更好?如果是,请举例。谢谢。
编辑 让我们以List
为例,它提供了ArrayList
的所有功能,并且是强类型的。那么什么时候我们需要使用ArrayList呢?也许有时它有更好的表现?我不知道。如果您能向我展示 ArrayList 的一些特别之处,我将不胜感激。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为对于新代码来说它应该被认为是有效过时的,但是没有令人信服的理由将其标记为过时并在 2.0 发布之前编写的所有代码中创建警告。
根据我的经验,大多数已被 Microsoft 标记为过时的类型和成员在某些方面都非常危险,如果您仍然有使用它们的代码库,则应该真正修复它们。虽然使用 ArrayList 很痛苦,并且(至少理论上)容易在执行时而不是编译时发现与类型相关的错误,但该类型足够好地完成其工作......通常确实没有令人信服的理由更改现有代码。当我碰巧正在处理使用 ArrayList 的代码区域时,我通常会考虑这种更改,而不是主动寻找它的每种用法。
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 usingArrayList
, rather than actively seeking out every usage of it.实际上它已完全从 Silverlight 中删除 - 所以意图就在那里。据推测,对于常规 .NET来说,有太多旧的现有代码使用
ArrayList
来废弃它,特别是因为很多人在运行时都将警告视为错误。如果没有充分的理由,您不应该在新代码中使用它。
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.
它本身并不“过时”。作为
'70'8090 年代初期的汽车,它已经“过时”了。如果我必须在List
举个例子:
终于有人对我的回答投了赞成票,所以我会添加一些内容:-)
如果你问“你会使用任何非通用集合吗”我的回答会有所不同。 哈希表有一个有趣的属性:
因此,在某些地方,
Hashtable
应该比lock + Dictionary
或ConcurrentDictionary
更好(但您必须对其进行基准测试)It isn't "obsolete" per se. It's "obsolete" as a
'70'80early '90 car. If I had to choose between aList<Object>
and anArrayList
there is a VERY VERY SMALL possibility I would use an ArrayList... Forget it... It doesn't implementIEnumerable<Object>
, so to use Linq I would have to use aOfType<Object>()
.To make an example:
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:
So there are places where an
Hashtable
should be better than alock + Dictionary
or aConcurrentDictionary
(but you would have to benchmark it)