ArrayList的VS名单,其中,对象> [英] ArrayList vs List<object>

查看:203
本文介绍了ArrayList的VS名单,其中,对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到乔恩在<一此回复href="http://stackoverflow.com/questions/386500/initialize-generic-object-with-unknown-type">Initialize与未知类型的通用对象:

I saw this reply from Jon on Initialize generic object with unknown type:

如果你想有一个单一的集合   包含多个不相关类型的   值,但是,你将不得不使用   名单,其中,对象&gt;

If you want a single collection to contain multiple unrelated types of values, however, you will have to use List<object>

我不是在比较的ArrayList VS 名单,其中;&GT; ,而的ArrayList VS 名单,其中,对象&gt; ,因为两者都将被暴露类型的元素对象。什么是在这种情况下,使用任何一个有什么好处?

I'm not comparing ArrayList vs List<>, but ArrayList vs List<object>, as both will be exposing elements of type object. What would be the benefit of using either one in this case?

编辑:这是这里的类型安全性没有问题,因为这两个类是暴露对象作为其项目。我们仍然需要从对象强制转换为所需的类型。我更感兴趣的东西不是类型安全等。

It's no concern for type safety here, since both class is exposing object as its item. One still needs to cast from object to the desired type. I'm more interested in anything other than type safety.

编辑:感谢马克Gravell和肖恩的答案。对不起,我只能挑1的答案,所以我就投了两个。

Thanks Marc Gravell and Sean for the answer. Sorry, I can only pick 1 as answer, so I'll up vote both.

推荐答案

您就可以直接使用LINQ扩展方法与名单,其中,对象&gt; ,但不与的ArrayList ,除非你注入一个演员LT;对象&gt;() / OfType&LT;对象&gt; (感谢的IEnumerable&LT;对象&gt; VS 的IEnumerable )。这是值得相当多的,即使你不需要类型安全等。

You'll be able to use the LINQ extension methods directly with List<object>, but not with ArrayList, unless you inject a Cast<object>() / OfType<object> (thanks to IEnumerable<object> vs IEnumerable). That's worth quite a bit, even if you don't need type safety etc.

速度将大致相同;结构仍将装箱,等 - 因此没有太多别的事情要告诉他们分开。除了我往往看到的ArrayList 为哎呀,有人写遗留code再......;-p

The speed will be about the same; structs will still be boxed, etc - so there isn't much else to tell them apart. Except that I tend to see ArrayList as "oops, somebody is writing legacy code again..." ;-p

这篇关于ArrayList的VS名单,其中,对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆