Daniel O'Connell said:
I agree, however I do suspect that 0 is defined the same or similarly. My
guess would be that the Enum cast doesn't check type, instead it simply
looks for a literal valued to 0. Whats more intresting, does something
like 0.0f or 0l result in the enum overload being called?
Good question. What the C# spec says is:
13.1.3 Implicit enumeration conversions
An implicit enumeration conversion permits the decimal-integer-literal 0
to be converted to any enum-type.
According to 9.4.4.2, the decimal-integer-literal 0 can be
0, possibly suffixed by one of U u L l UL Ul uL ul LU Lu lU lu
0x0, possilby suffixed the same way
or of course 00, 0x00, 000, 0x000, etc, again possibly suffixed. And I
think this is exactly what was intended. It's not that any integer values
are implicitly converted to enums. It's that there is a special constant
that's a member of all enum types, it means "no flags set", and it's
spelled "0".
As far as I can see, that's the only place the implicit conversion should
apply, so, in theory, 0l yes, 0f no.
Yes, I don't *think* there is anything in the spec that defines 0.0 as
implicitly convertible to an enum, however I havn't sat down and traced
the spec entirely, so I can't state that the spec doesn't define this.
I did some searching for "conversion", "cast:", etc. and didn't find
anything suggestive-looking.
However, in the spec or not, it is now effectivly a semantically required
portion of the language. Microsoft can't really change its compiler and
third party compiler vendors would have to support this in either case,
removing it will break code, unfortunatly.
There are ways to address this, e.g:
Version N+1 of the compiler issues a warning that the usage is
deprecated and will cause an error in future releases
Version N+2 issues an error unless a special compatibility-mode flag is
set
Version N+3 issues an error, period
But this is a small enough point that, I agree, there's no real point in
making the compiler stricter.