在C#和VB.NET铸造的区别区别、VB、NET

2023-09-03 03:27:32 作者:我是你的小星星

下面code正常工作在C#。

The following code works fine in C#.

    Int32 a, b;
    Int16 c;

    a = 0x7FFFFFFF;
    b = a & 0xFFFF;
    c = (Int16)b;

但是,这$ C有发生OverflowException $ C坠毁在 VB.NET 。

    Dim a, b As Int32
    Dim c As Int16

    a = &H7FFFFFFF
    b = a And &HFFFF
    c = CType(b, Int16)

这两个code片段似乎是一样的我。有什么区别,我怎么能得到C#code转换到VB.NET?

Both code snippets seem the same to me. What is the difference and how can I get the C# code converted to VB.NET?

推荐答案

从MSDN :

有关的运算,类型转换或转换操作引发发生OverflowException,操作必须发生在检查范围内。 默认情况下,在Visual Basic算术运算和溢出进行检查;在C#中,他们不是。如果发生在unchecked上下文中的操作,结果被丢弃任何高序位不适合目标类型截断。

For the arithmetic, casting, or conversion operation to throw an OverflowException, the operation must occur in a checked context. By default, arithmetic operations and overflows in Visual Basic are checked; in C#, they are not. If the operation occurs in an unchecked context, the result is truncated by discarding any high-order bits that do not fit into the destination type.

编辑: 如果你打算端口code,从C#到VB.NET,你可能会感兴趣的 它们之间的差异。同时比较和明确设置编译器设置是同在C#中的默认设置(需要时)

If you're going to port code from C# to VB.NET, you may be interested in differences between them. Also compare and explicitly set compiler settings to be the same as default settings in C# (when needed).