gpt4 book ai didi

c# System.OverflowException 异常

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:59 32 4
gpt4 key购买 nike

所以这是我的代码:

static void Main(string[] args)
{
Console.WriteLine("How many numbers are you going to enter?");

long num = long.Parse(Console.ReadLine());

long[] nums = new long[num];
}

当我为“num”输入 10000000000 时,我得到

"System.OverflowException Arithmetic operation resulted in an overflow."

我该怎么做才能修复它?

最佳答案

您的代码溢出是因为 C# 中数组的最大大小为 Int32.MaxValue ,等于 2147483647。您可以在 source code 中看到相关提示, 并在 documentation 中明确说明:

By default, the maximum size of an Array is 2 gigabytes (GB). In a 64-bit environment, you can avoid the size restriction by setting the enabled attribute of the configuration element (<gcAllowVeryLargeObjects> introduced in .NET 4.5) to true in the run-time environment. However, the array will still be limited to a total of 4 billion elements, and to a maximum index of 0X7FEFFFFF (2146435071) in any given dimension (0X7FFFFFC7 (2147483591) for byte arrays and arrays of single-byte structures).

关于c# System.OverflowException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34548429/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com