我正在转换为 VB.NET,它不提供 unchecked
关键字。但在这个声明中似乎没有必要:
const int dwAccess = unchecked((int)0xC0000000);
我在这里有两个观察结果:
dwAccess
声明为常量- 分配的值正好在
System.Int32
的范围内
考虑到这些,是否安全:
Const dwAccess As Integer = &HC0000000
在这种情况下使用它:
[DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
private static extern SafeFileHandle CreateFile(string lpFileName, int dwAccess, int dwShareMode,
IntPtr securityAttrs, int dwCreationDisposition,
int dwFlagsAndAttributes, IntPtr hTemplateFile);
澄清:这个问题不是关于unchecked
关键字在C#中是否必要的问题。 显然是。这是关于 VB.NET 中缺少关键字是否会妨碍语句的成功转换。
最佳答案
答案是肯定的。它是多余的,可以删除。
The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions.
更新,这个答案应该是合格的。
如果未选中原始 VB.NET 代码,则等效的 C# 代码也将以相同的方式发挥未选中的功能。两者都会溢出,并且都会给出相同的结果。
关于c# - 此 C# 语句的 VB.NET 版本是否需要 unchecked 关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50303185/