What is the Maximum Size that an Array can hold?
在C#2008中,数组可以容纳的最大大小是多少?
1 | System.Int32.MaxValue |
假设您的意思是
强制执行此限制是因为
完全不同的是,如果您担心这一点,很可能您正在使用大量数据,无论是正确还是不正确。在这种情况下,我会考虑使用
注意的另一个集合是
1 2 3 | Dictionary<int, string> foo = new Dictionary<int, string>(); foo[1000] ="Hello world!"; Console.WriteLine(foo[1000]); |
使用
根据MSDN,它是
默认情况下,阵列的最大大小为2 GB。
在64位环境中,可以通过在运行时环境中将gcAllowVeryLargeObjects配置元素的enabled属性设置为true来避免大小限制。
但是,该数组仍将限制为总共40亿个元素。
参考此处http://msdn.microsoft.com/zh-cn/library/System.Array(v=vs.110).aspx
注意:在这里,我通过假设我们将有足够的硬件RAM来关注数组的实际长度。
这个答案是关于.NET 4.5
根据MSDN,字节数组的索引不能大于2147483591。对于4.5之前的.NET,它也是数组的内存限制。在.NET 4.5中,此最大值是相同的,但对于其他类型,最大值可以为2146435071。
这是用于说明的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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; } |
以下是对您的问题的详细解答:
http://www.velocityreviews.com/forums/t372598-maximum-size-of-byte-array.html
您可能要提及所使用的.NET版本以及内存大小。
但是,对于您的应用程序,您将被限制在2G范围内,因此它取决于阵列中的内容。
我认为如果您不考虑VM,那就是Integer.MaxValue
我认为它与您的RAM(或可能是虚拟内存)空间相关联,并且绝对最大值受限于您的OS版本(例如32位或64位)
由于长度是一个整数,我会说Int.MaxValue