Why should I explicitly specify the hexadecimal value of the byte when using the ternary operator?

For instance:

byte a = 0x01; // Works fine
byte b = foo() ? 0x01 : 0x02; // Get error "Cannot impicitly convert int to byte"

Why is this so?

+4
source share
1 answer

When you assign a numeric literal (it does not matter whether it is hexadecimal or decimal) to a type variable byte, the compiler checks that the value matches the value byte. As soon as the compiler knows that the value is suitable, it processes the literal as if it had a type bytecorresponding to the type of the variable.

, , , . , . , int byte. , .

+2

All Articles