C#和VB.NET之间的区别 [英] Difference between casting in C# and VB.NET

查看:153
本文介绍了C#和VB.NET之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在C#中工作正常。

The following code works fine in C#.

    Int32 a, b;
    Int16 c;

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

但是这个代码在 VB.NET

    Dim a, b As Int32
    Dim c As Int16

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

这两个代码片段对我来说都是一样的。有什么区别,如何获得C#代码转换为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中的算术运算和溢出; 。如果操作发生在未检查的上下文中,则通过丢弃不适合目标类型的任何高阶位来截断结果。

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.

编辑:
如果您要将代码从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).

这篇关于C#和VB.NET之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆