什么等于“(字节)"?在VB.NET中? [英] What is the equivalent of "(byte)" in VB.NET?
问题描述
在VB.NET中等效于(byte)
:
C#:
uint值= 1161;字节数据=(字节)值;
数据= 137
VB.NET:
作为UInteger的Dim值= 1161Dim data1 As Byte = CType(value,Byte)Dim data2 As Byte = CByte(value)
异常:算术运算导致溢出.
如何获得与C#相同的结果?
默认情况下,C#不会检查整数溢出,但VB.NET会检查.
例如,如果您在C#中遇到相同的异常,将您的代码包装在 checked
块中:
已选中{uint值= 1161;字节数据=(字节)值;}
在您的VB.NET项目属性中,启用配置属性 => 优化 => 删除整数溢出检查,以及您的VB.NET代码将完全像您的C#代码一样工作.
然后为整个项目禁用整数溢出检查,但这通常不是问题.
What is the equivalent of (byte)
in VB.NET:
C#:
uint value = 1161;
byte data = (byte)value;
data = 137
VB.NET:
Dim value As UInteger = 1161
Dim data1 As Byte = CType(value, Byte)
Dim data2 As Byte = CByte(value)
Exception: Arithmetic operation resulted in an overflow.
How can I achieve the same result as in C#?
By default, C# does not check for integer overflows, but VB.NET does.
You get the same exception in C# if you e.g. wrap your code in a checked
block:
checked
{
uint value = 1161;
byte data = (byte)value;
}
In your VB.NET project properties, enable Configuration Properties => Optimizations => Remove Integer Overflow Checks, and your VB.NET code will work exactly like your C# code.
Integer overflow checks are then disabled for your entire project, but that's usually not a problem.
这篇关于什么等于“(字节)"?在VB.NET中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!