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