What is the equivalent of “(byte)” in VB.NET?

前端 未结 3 1894
刺人心
刺人心 2021-01-18 07:12

What is the equivalent of (byte) in VB.NET:

C#:

uint value = 1161;
byte data = (byte)value;

data = 137

VB.NET:<

3条回答
  •  广开言路
    2021-01-18 07:47

    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.

提交回复
热议问题