xll*_*xll 7 c# vb.net type-conversion
(byte)VB.NET中的等价物是什么:
uint value = 1161;
byte data = (byte)value;
Run Code Online (Sandbox Code Playgroud)
data = 137
Dim value As UInteger = 1161
Dim data1 As Byte = CType(value, Byte)
Dim data2 As Byte = CByte(value)
Run Code Online (Sandbox Code Playgroud)
例外:算术运算导致溢出.
如何在C#中获得相同的结果?
slo*_*oth 11
默认情况下,C#不检查整数溢出,但VB.NET会检查.
如果你将代码包装在一个checked块中,你会在C#中获得相同的异常:
checked
{
uint value = 1161;
byte data = (byte)value;
}
Run Code Online (Sandbox Code Playgroud)
在您的VB.NET项目属性中,启用Configuration Properties => Optimizations => Remove Integer Overflow Checks,您的VB.NET代码将与您的C#代码完全相同.
然后为整个项目禁用整数溢出检查,但这通常不是问题.
首先尝试从数字中删除最重要的字节,然后将其转换为字节:
Dim value As UInteger = 1161
Dim data1 As Byte = CType(value And 255, Byte)
Dim data2 As Byte = CByte(value And 255)
Run Code Online (Sandbox Code Playgroud)