Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top