Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem here is type inference. The compiler is trying to find out which overload to use based on the argument, but it's also trying to find out what the type of the argument is based on the chosen overload. In the case of <code>M(() =&gt; DynamicObject())</code>, the process goes something like this:</p> <ol> <li>The argument to the method is a lambda with zero parameters. This gives us all three overloads as possibilities.</li> <li>The body of the lambda returns <code>dynamic</code>. Because there is an implicit conversion from <code>dynamic</code> to any other type, we now know all three overloads are good.</li> <li>Try choosing the best overload. In most cases, “best” means the most derived type. Because both <code>int</code> and <code>string</code> derive from <code>object</code>, the overloads with <code>int</code> and <code>string</code> are considered best.</li> <li>We now have two “best” overloads, which means the compiler can't actually choose one of them. The compilation fails.</li> </ol> <p>Now, regarding possible solutions to your problem:</p> <ol> <li><p>Make the type of the lambda explicit, either using cast or typed local variable:</p> <pre><code>M((Func&lt;dynamic&gt;)(() =&gt; DynamicObject())); </code></pre> <p>or</p> <pre><code>Func&lt;dynamic&gt; f = () =&gt; DynamicObject(); M(f); </code></pre></li> <li><p>Rename the dynamic overload to something like <code>DynamicM</code>. This way, you don't have to deal with overload resolution.</p></li> <li><p>This one feels somewhat wrong to me: make sure the <code>dynamic</code> overload is the only one that fits, by casting to <code>object</code>:</p> <pre><code>M(() =&gt; (object)DynamicObject()) </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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