【发布时间】:2011-04-23 20:12:32
【问题描述】:
我可以向 List 添加多少值?
例如:
List<string> Item = runtime data
数据大小不固定。它可能是 10 000 或超过 1 000 000。我已经用 Google 搜索过,但没有找到确切的答案。
【问题讨论】:
我可以向 List 添加多少值?
例如:
List<string> Item = runtime data
数据大小不固定。它可能是 10 000 或超过 1 000 000。我已经用 Google 搜索过,但没有找到确切的答案。
【问题讨论】:
理论上,List<T> 的当前实现中可以存储的最大元素数为 Int32.MaxValue - 刚刚超过 20 亿。
在当前 Microsoft 的 CLR 实现中,最大对象大小限制为 2GB。 (其他实现,例如 Mono,可能没有这个限制。)
您的特定列表包含字符串,它们是引用类型。引用的大小将是 4 或 8 个字节,具体取决于您是在 32 位还是 64 位系统上运行。这意味着您可以存储的字符串数量的实际限制在 32 位上约为 5.36 亿,在 64 位上约为 2.68 亿。
实际上,在达到这些限制之前,您很可能会用完可分配内存,尤其是在 32 位系统上运行时。
【讨论】:
2147483647 因为 List 外的所有函数都使用 int。
来自 mscorlib:
private T[] _items;
private int _size;
public T this[int index]
{
get
{
//...
}
}
【讨论】:
list.Count() 属性是 int32,因此它必须是 int32 的最大限制,但您的列表在此限制上的表现是一个很好的观察。
如果你做一些列表操作,理论上会更线性。
我会说,如果您有大量关于 .net 4.0 中的并行集合的项目,这将使您的列表操作更具响应性。
【讨论】:
根据List的实现
private void EnsureCapacity(int min) {
if (_items.Length < min) {
int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;
// Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
if (newCapacity < min) newCapacity = min;
Capacity = newCapacity;
}
}
现在导航到这个 Array.MaxArrayLength:
internal const int MaxArrayLength = 2146435071;
【讨论】:
您可以使用 List
【讨论】: