Mat*_*ley 48
System.Int32.MaxValue
Run Code Online (Sandbox Code Playgroud)
假设你的意思System.Array,即.任何正常定义的数组(int[]等).这是数组可以容纳的最大值.每个值的大小仅受可用于保存它们的内存量或虚拟内存量的限制.
强制执行此限制是因为System.Array使用Int32as作为索引器,因此只能使用an 的有效值Int32.除此之外,可以仅使用正值(即>= 0).这意味着数组大小的绝对最大上限是a的值的绝对最大上限Int32,可用于Int32.MaxValue并且相当于2 ^ 31,或大约20亿.
在完全不同的情况下,如果您担心这一点,很可能您正在使用大量数据,无论是正确还是错误.在这种情况下,我会考虑使用a List<T>而不是数组,这样你只需要使用尽可能多的内存.事实上,我建议一直使用一种List<T>或另一种通用集合类型.这意味着只会分配您实际使用的内存,但您可以像使用普通数组一样使用它.
另一个音符集合Dictionary<int, T>也可以像普通数组一样使用,但只会稀疏地填充.例如,在以下代码中,将只创建一个元素,而不是数组将创建的1000个元素:
Dictionary<int, string> foo = new Dictionary<int, string>();
foo[1000] = "Hello world!";
Console.WriteLine(foo[1000]);
Run Code Online (Sandbox Code Playgroud)
使用Dictionary还可以控制索引器的类型,并允许您使用负值.对于绝对最大尺寸的稀疏数组,您可以使用a Dictionary<ulong, T>,这将提供比您可能想到的更多潜在元素.
Sai*_*Sai 18
每个MSDN都是
默认情况下,阵列的最大大小为2千兆字节(GB).
在64位环境中,可以通过在运行时环境中将gcAllowVeryLargeObjects配置元素的enabled属性设置为true来避免大小限制.
但是,阵列仍将限制为总共40亿个元素.
请参阅此处http://msdn.microsoft.com/en-us/library/System.Array(v=vs.110).aspx
注意:这里我假设我们将拥有足够的硬件RAM,专注于数组的实际长度.
Ant*_*n K 12
这个答案是关于.NET 4.5的
根据MSDN,字节数组的索引不能大于2147483591.对于4.5之前的.NET,它也是数组的内存限制.在.NET 4.5中,此最大值相同,但对于其他类型,最大值可达2146435071.
这是用于说明的代码:
static void Main(string[] args)
{
// -----------------------------------------------
// Pre .NET 4.5 or gcAllowVeryLargeObjects unset
const int twoGig = 2147483591; // magic number from .NET
var type = typeof(int); // type to use
var size = Marshal.SizeOf(type); // type size
var num = twoGig / size; // max element count
var arr20 = Array.CreateInstance(type, num);
var arr21 = new byte[num];
// -----------------------------------------------
// .NET 4.5 with x64 and gcAllowVeryLargeObjects set
var arr451 = new byte[2147483591];
var arr452 = Array.CreateInstance(typeof(int), 2146435071);
var arr453 = new byte[2146435071]; // another magic number
return;
}
Run Code Online (Sandbox Code Playgroud)