.NET:ArrayList 与 List

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/365482/
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-09-03 10:48:43  来源:igfitidea点击:

.NET: ArrayList vs List

.netvb.netcollections

提问by MOZILLA

What is the difference between ArrayList and List in VB.NET

VB.NET中ArrayList和List有什么区别

回答by Mike Scott

ArrayLists are essentially deprecated as they're untyped - you need to use casts with them - and they're slower and less space efficient for value types because they require the items to be boxed.

ArrayLists 基本上已被弃用,因为它们是无类型的——你需要对它们使用强制转换——而且它们对于值类型来说速度较慢,空间效率较低,因为它们需要对项目进行装箱。

Generic lists were introduced with .Net 2.0 and are the way to go. Often a List is better than an array, with few downsides.

.Net 2.0 引入了通用列表,这是要走的路。通常,列表比数组要好,但缺点很少。

As these collections are part of the .Net Base Class Library, this advice also applies to C# and to any .Net language which supports generics - it's not specific to VB.NET.

由于这些集合是 .Net 基类库的一部分,因此该建议也适用于 C# 和任何支持泛型的 .Net 语言 - 它不是特定于 VB.NET。

回答by arul

List is a generic implementation of ArrayList. ArrayList stores all objects as System.Objectwhich you need then cast to appropriate type. ArrayLists are heterogenous, List can store only one type of objects - that type supplied as its generic parameter.

List 是 ArrayList 的通用实现。ArrayList 将所有对象存储为System.Object,然后您需要将其转换为适当的类型。ArrayLists 是异构的,List 只能存储一种类型的对象 - 作为其通用参数提供的类型。

List<string> strList; // can store only strings
List<int> intList; // can store only ints
ArrayList someList; // can store anything

回答by xslr

ArrayLists are even more space inefficient when used on 64bit to store primitive elements because of the 64bit wide memory references as opposed to 32bit references on 32bit machines, and boxing.

ArrayList 在 64 位上用于存储原始元素时,空间效率甚至更低,因为 64 位宽的内存引用与 32 位机器上的 32 位引用和装箱相反。

See this for more details: http://blogs.msdn.com/joshwil/archive/2004/04/13/112598.aspx

有关更多详细信息,请参阅:http: //blogs.msdn.com/joshwil/archive/2004/04/13/112598.aspx

回答by ggf31416

ArrayList allows you to write this:

ArrayList 允许你这样写:

Dim customers as new ArrayList
Dim c as new Customer
Dim m as new Manager
customers.Add(c)
customers.Add(m)

'This will cause an exception '  
For each c as Customer in customers
console.writeline(c.Name)
Next

A List(of Customer) allows only object of Customer type and types that inherit from Customer, so you cannot make such mistakes.

一个 List(of Customer) 只允许 Customer 类型的对象和从 Customer 继承的类型,所以你不能犯这样的错误。

Even if you need to put objects of unrelated types in the same collection, a List(Of Object) is a better choice as it makes explicit that you are dealing with different types.

即使您需要将不相关类型的对象放在同一个集合中,List(Of Object) 也是更好的选择,因为它明确表示您正在处理不同的类型。

回答by Kibbee

List can make use of generics so that only objects of specific types can be placed into it, so that you can have extra type checking and so that you can cut down on processing time due to boxing and unboxing. Arraylist cannot use this. In almost all cases, you'll want to use a List rather than an Arraylist.

List 可以利用泛型,因此只能将特定类型的对象放入其中,这样您就可以进行额外的类型检查,从而减少由于装箱和拆箱而导致的处理时间。Arraylist 不能使用它。在几乎所有情况下,您都希望使用 List 而不是 Arraylist。