gpt4 book ai didi

c# - UInt64 和 "The operation overflows at compile time in checked mode"- CS0220

转载 作者:可可西里 更新时间:2023-11-01 08:03:15 26 4
gpt4 key购买 nike

这感觉像是一个愚蠢的问题,但我似乎看不到答案。我有一个 UInt64,它的最大值应该是

UInt64.MaxValue 18446744073709551615

但是,当我尝试分配一个中等大小的数字时,出现“在检查模式下编译时操作溢出”的溢出错误。如果我将它包装在一个“未经检查”的 block 中,那么它会编译并运行,就像这个变量为零一样:

UInt64 value1 = 1073741824 * 8; // Compile error CS0220
UInt64 value2 = 8589934592; // Actual value - no error

为什么会这样?

最佳答案

因为:

UInt64 value1 = 1073741824 * 8;

作为带符号的 32 位整数进行算术运算,然后将其转换为 ulong。尝试:

UInt64 value1 = 1073741824UL * 8;

UL 表示文字是无符号长整型。有关文字后缀的更多信息,请参阅 C# 规范的第 2.4.4 节:

If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong

关于c# - UInt64 和 "The operation overflows at compile time in checked mode"- CS0220,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10167219/

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