C#3 (Visual Studio 2008) introduced a breaking change to the language (http://msdn.microsoft.com/en-us/library/cc713578.aspx, see change 12) that allows any literal zero to be implicitly converted to an Enum. This seems odd in many ways. Does anyone know why this is part of the spec? Why not a literal one? Or seven? Why is zero special? And it makes some very counterintuitive overload resolution choices. For instance.
function string F(object o) { return o.ToString(); }
function string F(DbType t) { return t.ToString(); }
int i = 0;
F((long)0) == "String" // would have been "0" in VS 2005
F(0) == "String"
F(i) == "0"
Very confusing, and an intentionally introduced breaking change to the language. Any ideas?
C# has always allowed the implicit conversion of the literal 0 to any Enum value. What has changed is how the rule is applied to other constant expressions. It was made to be more consistent and allow any constant expressions which evaluates to 0 to be implicitly convertible to an enum.
The example you gave produces the same behavior in all versions of C#.
Here is an example of changed behavior (directly from the linked documentation)
Visual C# 2005 output:
Visual C# 2008 output: