There doesn't seem to be a way to use C#'s ternary operator on two bytes like so:
byte someByte = someBoolean ? 0 : 1;
That code currently fails to compile with "Cannot convert source type 'int' to target type 'byte'", because the compiler treats the numbers as integers. Apparently there is no designated suffix to indicate that 0 and 1 are bytes, so the only workarounds are to (a) cast the result into a byte or (b) to use an if-else control after all.
Any thoughts?
The cast is not a problem here, in fact, the IL code should not have a cast at all.
Edit: The IL generated looks like this:
That compiles OK on VS2008.
Correction: This compiles OK in VS2008:
But this does not:
Odd!
Edit: Following Eric's advice (see his comment below), I tried this:
And it compiles perfectly. Not that I distrust Eric; I just wanted to include this here for the sake of completeness.
You could always do:
This will yield myByte == 0 for false and myByte == 1 for true.