作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这感觉像是一个愚蠢的问题,但我似乎看不到答案。我有一个 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/
我是一名优秀的程序员,十分优秀!