This is a bug you can use 0.0. The compiler implicitly processes all constant expressions with a null value as 0.
Now it’s right for the compiler to allow implicit conversion from the constant expression int 0 to your enum in accordance with section 6.1.3 of the C # 5 specification:
Implicit conversion of enumerations allows you to convert a decimal literal integer 0 to any type of enumeration and any type with a null type, the base type of which is an enumeration type. In the latter case, the conversion is evaluated by converting to the base type of enumeration and wrapping the result (§4.1.10).
I spoke with the C # team about this before: they would like to remove the random conversion from 0.0 (and indeed 0.0m and 0.0f) to enum values, but unfortunately I realized that it broke too much code - although it never should have been allowed first.
The Mono mcs compiler disallows all these floating point conversions, although this allows:
const int Zero = 0; ... SomeEnum x = Zero;
even though Zero is a constant expression, but not a decimal integer literal.
I would not be surprised to see a change to the C # specification in the future to allow any integer constant expression with a value of 0 (i.e. mimic mcs ), but I would not expect floating point conversions to ever be officially correct. (I was mistaken earlier to predict the future of C #, of course ...)
Jon Skeet May 20 '14 at 14:28 2014-05-20 14:28
source share