Conditional operator cannot cast implicitly?

前端 未结 3 1791
再見小時候
再見小時候 2020-11-22 07:14

I\'m a little stumped by this little C# quirk:

Given variables:

Boolean aBoolValue;
Byte aByteValue;

The following compiles:

<
3条回答
  •  天命终不由人
    2020-11-22 07:46

    I may not have a great answer for you, but if you do this in many places, you could declare:

    private static readonly Byte valueZero = (byte)0;
    private static readonly Byte valueOne = (byte)1;
    

    and just these variables. You might get away with using const if it is local to the project.

    EDIT: using readonly would not make sense - these aren't ever meant to change.

提交回复
热议问题