声明为最大大小的数组在C#

问题描述

我知道类似的问题已经问了很多遍,并且也得到了回答。 互联网上的许多文章仅指向一个答案。最高int2147483647

内联是来自stackoverflow的几个链接:

What is the Maximum Size that an Array can hold?

Maximum size of string array in C#

但是,当我尝试检查相同内容时,会抛出OutOfMemoryException。实际上,我尝试将大小减小2到1073741823(抛出异常),将大小减小4到536870911(抛出异常),将大小减小8到268435455(程序执行毫无例外)。

测试代码:

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            int arraySize = Int32.MaxValue / 1; //Factor when set to 8,program executes without any exception
            int[] testArray = new int[arraySize];

            testArray[0] = 6;

            Console.WriteLine("Press Enter to finish.");
            Console.ReadLine();
        }
    }
}

看起来所有其他答案都指向一个理论值,即2147483647。但是,实际上并非总是如此。因此,问题是,鉴于应用程序代码非常简单,只需一个数组和几次控制台调用,是否有可能弄清楚应用程序将抛出OutOfMemoryException的数组大小。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)