Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes VS2010 not comply with "Evaluation of user-defined conversions" in C# 4.0 spec?
    text
    copied!<p>If you compile the following code with Visual Studio 2010:</p> <pre><code> 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; } </code></pre> <p>using ILSpy, <code>return (B)a;</code> is actually compiled as <code>return A.op_Implicit(a.value)</code>.</p> <p>By my understanding of C# 4.0 chapter 6.4.5 'User-defined explicit conversions', it should produce a compiler error.</p> <p>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.</p> <p>C# 4.0:</p> <blockquote> <p>Find the set of applicable user-defined and lifted conversion operators, U. This set consists of the <strong>user-defined and lifted</strong> 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.</p> </blockquote> <p>ECMA 334:</p> <blockquote> <p>Find the set of applicable conversion operators, U. This set consists of the user-defined and, <strong>if S and T are both nullable</strong>, 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.</p> </blockquote> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload