C# ArrayList 与 List<object>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/391088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:42:14  来源:igfitidea点击:

ArrayList vs List<object>

c#.netlistarraylist

提问by faulty

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

我看到了 Jon 关于用未知类型初始化通用对象的回复:

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

但是,如果您希望单个集合包含多个不相关类型的值,则必须使用 List<object>

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

我不是在比较ArrayListvs List<>,而是ArrayList比较 vs List<object>,因为两者都会暴露 type 的元素object。在这种情况下使用任何一个有什么好处?

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

编辑:这里不关心类型安全,因为两个类都object作为它的项目公开。仍然需要转换object为所需的类型。除了类型安全之外,我对任何事情都更感兴趣。

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

编辑:感谢 Marc Gravell 和 Sean 的回答。抱歉,我只能选择 1 作为答案,所以我会同时投票。

采纳答案by Marc Gravell

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.

您将能够直接使用 LINQ 扩展方法List<object>,但不能使用ArrayList,除非您注入Cast<object>()/ OfType<object>(感谢IEnumerable<object>vs IEnumerable)。即使您不需要类型安全等,这也是值得的。

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 ArrayListas "oops, somebody is writing legacy code again..." ;-p

速度将大致相同;结构仍然会被装箱,等等 - 所以没有什么可以区分它们的。除了我倾向于ArrayList认为“哎呀,有人又在写遗留代码了……”;-p

回答by Funky81

List<> is a typesafe version of ArrayList. It will guarantee that you will get the same object type in the collection.

List<> 是 ArrayList 的类型安全版本。它将保证您将在集合中获得相同的对象类型。

回答by Tamas Czinege

Yes, besides being typesafe, generic collections might be actually faster.

是的,除了类型安全之外,泛型集合实际上可能更快。

From the MSDN (http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx)

从 MSDN ( http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx)

The System.Collections.Genericnamespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.

System.Collections.Generic命名空间包含接口和定义泛型集合,它允许用户创建强类型集合能提供比非泛型强类型集合更好的类型安全和性能等级。

回答by Sean

One big benefit to using List<object>is that these days most code is written to use the generic classes/interfaces. I suspect that these days most people would write a method that takes a IList<object>instead of an IList. Since ArrayListdoesn't implement IList<object>you wouldn't be able to use an array list in these scenarios.

使用的一大好处List<object>是,如今大多数代码都是为使用通用类/接口而编写的。我怀疑现在大多数人会编写一个使用 aIList<object>而不是IList. 由于ArrayList没有实现,IList<object>您将无法在这些场景中使用数组列表。

I tend to think of the non-generic classes/interfaces as legacy code and avoid them whenever possible.

我倾向于将非通用类/接口视为遗留代码,并尽可能避免使用它们。

回答by Lasse V. Karlsen

In this case, ArrayListvs. List<Object>then you won't notice any differences in speed. There might be some differences in the actual methods available on each of these, particular in .NET 3.5 and counting extension methods, but that has more to do with ArrayList being somewhat deprecated than anything else.

在这种情况下,ArrayListList<Object>then 您不会注意到速度上的任何差异。每个可用的实际方法可能存在一些差异,特别是在 .NET 3.5 和计数扩展方法中,但这更多地与 ArrayList 比其他任何东西都有些过时有关。

回答by tuinstoel

Do some benchmarking and you will know what performs best. I guestimate that the difference is very small.

做一些基准测试,你就会知道什么表现最好。我估计差异非常小。