문제

Why does the following code print out its below value, specifically with regards to the CShort method in VB.Net?

Console.WriteLine(CShort(False)) 'prints 0
Console.WriteLine(CShort(True)) 'prints -1

I have been able to reproduce this with many of the Visual Basic type conversions.

Was this just a design decision in Visual Basic?

도움이 되었습니까?

해결책

In Visual Basic, the Boolean data type is stored as a 16 bit signed integer.

In this representation , -1 evaluates to 16 one bits and 0 is, of course, 16 zero bits.

Hence, if you want the relationship True = Not False to hold, then True must have the value -1.

다른 팁

It's not a VB-specific thing - all VB does is implicitly cast it for you to short. In C#, you get the same result with an explicit call to a System.Convert method - e.g., System.Convert.ToInt16(true).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top