If you compile the following code with Visual Studio 2010:
public struct A
{
public static implicit operator B(A a)
{
Console.WriteLine("11111111111");
return new B();
}
}
public struct B
{ }
public static B F(A? a)
{
return (B)a;
}
using ILSpy, return (B)a;
is actually compiled as return A.op_Implicit(a.value)
.
By my understanding of C# 4.0 chapter 6.4.5 'User-defined explicit conversions', it should produce a compiler error.
But, reading ECMA 334 chapter 13.4.4 'User-defined explicit conversions', it has a different rule which the above code seems to comply with.
C# 4.0:
Find the set of applicable user-defined and lifted conversion operators, U. This set consists of the user-defined and lifted implicit or explicit conversion operators declared by the classes or structs in D that convert from a type encompassing or encompassed by S to a type encompassing or encompassed by T. If U is empty, the conversion is undefined and a compile-time error occurs.
ECMA 334:
Find the set of applicable conversion operators, U. This set consists of the user-defined and, if S and T are both nullable, lifted implicit or explicit conversion operators (§13.7.3) declared by the classes or structs in D that convert from a type encompassing or encompassed by S to a type encompassing or encompassed by T. If U is empty, there is no conversion, and a compile-time error occurs.
Am I correct that VS2010 does not comply with the "Evaluation of user-defined conversions" section in the C# 4.0 spec, but does comply with the ECMA spec?