在c#中,数组列表和List<>有什么区别?
是不是只有List<>有类型而ArrayList没有?
在c#中,数组列表和List<>有什么区别?
是不是只有List<>有类型而ArrayList没有?
当前回答
要添加的另一个区别是线程同步。
ArrayList provides some thread-safety through the Synchronized property, which returns a thread-safe wrapper around the collection. The wrapper works by locking the entire collection on every add or remove operation. Therefore, each thread that is attempting to access the collection must wait for its turn to take the one lock. This is not scalable and can cause significant performance degradation for large collections. List<T> does not provide any thread synchronization; user code must provide all synchronization when items are added or removed on multiple threads concurrently.
更多信息在这里线程同步在.Net框架
其他回答
ArrayList不是类型安全的,而List<T>是类型安全的。简单:)。
如.NET Framework文档中所述
我们不建议你将数组列表类用于new 发展。相反,我们建议您使用通用的List<T> 类。ArrayList类被设计用来保存异构类 对象的集合。然而,它并不总是提供最好的 的性能。相反,我们建议如下: 对于异构对象集合,使用List<Object>(在c#中)或List(of Object)(在Visual Basic中)类型。 对于对象的同构集合,使用List<T>类。
请参见不应使用非泛型集合
ArrayList是不同类型数据的集合,而List<>是其自身依赖的相似类型数据的集合。
再加上以上几点。在64位操作系统中使用ArrayList占用的内存是32位操作系统的2倍。同时,泛型列表list <T>占用的内存比ArrayList少得多。
例如,如果我们在32位中使用一个19MB的数组列表,那么在64位中它将占用39MB。但是如果你有一个32位的8MB的通用列表list <int>,它在64位只需要8.1MB,这与ArrayList相比相差了481%。
来源:数组列表与通用列表的基本类型和64位
是的,差不多。List<T>是泛型类。它支持存储特定类型的值,而不强制转换为对象(当T是ArrayList情况下的值类型时,这会引起装箱/拆箱开销)。ArrayList仅存储对象引用。作为一个泛型集合,List<T>实现了泛型IEnumerable<T>接口,可以很容易地在LINQ中使用(不需要任何Cast或OfType调用)。
ArrayList属于c#没有泛型的时代。不赞成使用List<T>。你不应该在以。net >= 2.0为目标的新代码中使用ArrayList,除非你必须与使用它的旧API进行交互。