I always thought this part of C# was kind of hokey, but after reporting it to Microsoft as a bug (derp), and it getting escalated to the framework team, it was revealed, it is part of the spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf[^] (The C# Specification), page 136 states: "An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type." So the short answer is, it's in the spec. The long answer is (from Habib, here: http://stackoverflow.com/questions/14950750/why-switch-for-enum-accepts-implicit-conversion-to-0-but-no-for-any-other-intege[^]) At compile time 0 is known as the default value for an enum. No other value is explicitly known, and that is why no other value is allowed to be substituted. In fact, you can try some sample code out for yourself:
class Program
{
enum Direction { left = 1, right = 2 }
static void Main(string\[\] args)
{
Direction d = 0;
Console.WriteLine("Direction: " + Enum.GetName(typeof(Direction),d));
Console.ReadLine();
}
}
Note that even though we've explicitly excluded 0 from our example, the code still compiles and works happily returning "" for the label corresponding to 0. It's always going to be there, and that's why 0 is allowed an implicit cast.
============================= I'm a developer, he's a developer, she's a developer, Wouldn't ya like to be a developer too?