C# 数组与数组列表的显着差异?

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

Significant differences in Array vs Array List?

c#arraysarraylist

提问by Mike Olson

Possible Duplicate:
When to use ArrayList over array[] in c#?

可能的重复:
何时在 C# 中使用 ArrayList 而不是 array[]?

From the perspective of memory or processor costs, does there appear to be a significant difference between an array and an arrayList object?

从内存或处理器成本的角度来看,数组和 arrayList 对象之间似乎存在显着差异?

回答by Asik

An array is a low-level data structure that essentially maps to a region in memory. An ArrayListis a variable length list implemented as an array of objectthat is re-allocated as the list grows.

数组是一种底层数据结构,本质上映射到内存中的一个区域。AnArrayList是一个可变长度列表,作为一个数组实现object,随着列表的增长而重新分配。

ArrayListtherefore has some overhead related to managing the size of the internal array, and more overhead related to casting objects to the correct type when you access the list.

ArrayList因此有一些与管理内部数组大小相关的开销,以及更多与访问列表时将对象转换为正确类型相关的开销。

Also, storing everything as objectmeans that value types get boxed on write and unboxed on read, which is extremely detrimental to performance. Using List<T>, a similar but strongly-typed variable size list avoids this issue.

此外,将所有内容存储为object意味着值类型在写入时被装箱并在读取时被取消装箱,这对性能极为不利。使用List<T>,一个类似但强类型的可变大小列表避免了这个问题。

In fact, ArrayListis practically deprecated in favor of List<T>since .NET 2.0.

实际上,从 .NET 2.0 开始ArrayList实际上已弃用List<T>

回答by Dai

An array is a contiguous block of memory of fixed size, whereas an ArrayList (though you should prefer List since .NET 2.0) wraps an array to provide dynamically-resizable storage.

数组是固定大小的连续内存块,而 ArrayList(尽管从 .NET 2.0 开始您应该更喜欢 List)包装数组以提供可动态调整大小的存储。

The "difference" between them being that, as far as they're encapsulated, an ArrayList is resizable, an array isn't. As far as the implementation is concerned: because an ArrayList wraps (and reallocates) arrays it will require more slightly more memory than an array (as it has to know the current number of elements, as opposed to its capacity), furthermore an ArrayList also requires CPU time to reallocate and copy its internal array if it ever reaches its internal capacity.

它们之间的“区别”在于,就它们被封装而言,ArrayList 是可调整大小的,而数组则不是。就实现而言:因为 ArrayList 包装(并重新分配)数组,所以它需要比数组稍微多一点的内存(因为它必须知道当前元素的数量,而不是它的容量),而且 ArrayList 也如果它达到其内部容量,则需要 CPU 时间来重新分配和复制其内部阵列。

However, instantiating an ArrayList is no more expensive than allocating an array. The only difference there being the handful of instructions needed to initialize the ArrayList's state. The difference is negligible and not worth worrying about.

然而,实例化一个 ArrayList 并不比分配一个数组更昂贵。唯一的区别是初始化 ArrayList 的状态所需的少量指令。差异可以忽略不计,不值得担心。

You'll find that if you are reallocating an array by yourself as the means of creating a resizable collection then you're better off using ArrayList/List as it has been thoroughly tested.

您会发现,如果您自己重新分配数组作为创建可调整大小的集合的方法,那么最好使用 ArrayList/List,因为它已经过全面测试。