C# 规范是否允许从 ulong 转换为 int? Monotouch 给出一个 NumberOverflow

Does C# spec permit casting from a ulong to an int? Monotouch gives a NumberOverflow

我在几个环境中测试了以下代码

ulong c = 10000000000000000;  // Base 10
int w = (int) c;              // w should be 1874919424

结果如下:

VS2013 .NET 4.5         -   w = 1874919424
Xamarin (mono) Android  -   w = 1874919424
Xamarin (mono) iOS      -   number overflow

此方法在 BouncyCastle 加密中广泛使用,我正在将其移植到 C# 并希望根据需要进行尽可能少的更改。

如果编译完全相同,则所有平台上的行为应该相同,并且不应与 unified/classic 32/64 位或 nint 支持相关。

因此请确保您的 iOS 项目未在启用 生成溢出检查 的情况下构建。

在 Xamarin Studio 的“项目选项”、“生成”、“编译器”中或在提供给编译器的命令行选项中查找:/checked.