为什么 ArrayList 没有标记为 [Obsolete]?

发布于 2024-10-18 11:45:49 字数 451 浏览 8 评论 0原文

经过深思熟虑并研究 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稀香 2024-10-25 11:45:50

我认为对于新代码来说它应该被认为是有效过时的,但是没有令人信服的理由将其标记为过时并在 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 using ArrayList, rather than actively seeking out every usage of it.

老街孤人 2024-10-25 11:45:50

实际上它已完全从 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.

亢潮 2024-10-25 11:45:50

它本身并不“过时”。作为 '70 '80 90 年代初期的汽车,它已经“过时”了。如果我必须在 ListArrayList 之间进行选择,那么我会使用 ArrayList 的可能性非常非常小...算了...它不没有实现 IEnumerable,因此要使用 Linq,我必须使用 OfType()。

举个例子:

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);

终于有人对我的回答投了赞成票,所以我会添加一些内容:-)

如果你问“你会使用任何非通用集合吗”我的回答会有所不同。 哈希表有一个有趣的属性:

哈希表是线程安全的,可供多个读取线程和单个写入线程使用。当只有一个线程执行写入(更新)操作时,它对于多线程使用是线程安全的,只要写入者被序列化到哈希表,就允许无锁读取。

因此,在某些地方,Hashtable 应该比 lock + DictionaryConcurrentDictionary 更好(但您必须对其进行基准测试)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文