ArrayList的VS名单<>在C#中
什么是的ArrayList
和列表℃之间的差异;在C#
;&GT?
难道只有列表<>
有一个类型,而的ArrayList
不
Is it only that List<>
has a type while ArrayList
doesn't?
是,pretty得多。 列表&LT; T&GT;
是一个泛型类。它支持存储特定类型的值,而无需进行转换,或从对象
(这将招致装箱/拆箱的开销时, T
是的ArrayList
情况下)值类型。 的ArrayList
只需商店对象
引用。作为一个泛型集合,它实现了通用的IEnumerable&LT; T&GT;
接口,可以在LINQ很容易地使用(无需任何铸造
或 OfType
调用)。
Yes, pretty much. List<T>
is a generic class. It supports storing values of a specific type without casting to or from object
(which would have incurred boxing/unboxing overhead when T
is a value type in the ArrayList
case). ArrayList
simply stores object
references. As a generic collection, it implements the generic IEnumerable<T>
interface and can be used easily in LINQ (without requiring any Cast
or OfType
call).
的ArrayList
所属的天C#没有泛型。这是有利于的pcated德$ P $列表与LT; T&GT;
。你不应该的ArrayList
在新的code的目标使用.NET> = 2.0,除非你有一个使用它的一个旧的API接口。
ArrayList
belongs to the days that C# didn't have generics. It's deprecated in favor of List<T>
. You shouldn't use ArrayList
in new code that targets .NET >= 2.0 unless you have to interface with an old API that uses it.