Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>Note: Not really an answer, but far too big for a comment.</em></p> <p>More interesting results when you throw in type-inference. Consider this code:</p> <pre><code>public class Test { public static void Blah&lt;T&gt;(Action&lt;T&gt; blah) { } public static void Main() { Blah(x =&gt; { Console.LineWrite(x); }); } } </code></pre> <p>It won't compile, because there's no good way to infer what <code>T</code> should be.<br> <strong>Error Message</strong>:</p> <blockquote> <p>The type arguments for method <code>'Test.Blah&lt;T&gt;(System.Action&lt;T&gt;)'</code> cannot be inferred from the usage. Try specifying the type arguments explicitly.</p> </blockquote> <p>Makes sense. Let's specify the type of <code>x</code> explicitly and see what happens:</p> <pre><code>public static void Main() { Blah((int x) =&gt; { Console.LineWrite(x); }); } </code></pre> <p>Now things go awry because <code>LineWrite</code> doesn't exist.<br> <strong>Error Message</strong>:</p> <blockquote> <p>'System.Console' does not contain a definition for 'LineWrite'</p> </blockquote> <p>Also sensible. Now let's add in named arguments and see what happens. First, without specifying the type of <code>x</code>:</p> <pre><code>public static void Main() { Blah(blah: x =&gt; { Console.LineWrite(x); }); } </code></pre> <p>We would expect to get an error message about not being able to infer type arguments. And we do. <em>But that's not all</em>.<br> <strong>Error Messages</strong>:</p> <blockquote> <p>The type arguments for method <code>'Test.Blah&lt;T&gt;(System.Action&lt;T&gt;)'</code> cannot be inferred from the usage. Try specifying the type arguments explicitly.</p> <p>'System.Console' does not contain a definition for 'LineWrite'</p> </blockquote> <p>Neat. Type inference fails, <em>and</em> we're told exactly why the lambda conversion failed. Ok, so let's specify the type of <code>x</code> and see what we get:</p> <pre><code>public static void Main() { Blah(blah: (int x) =&gt; { Console.LineWrite(x); }); } </code></pre> <p><strong>Error Messages</strong>:</p> <blockquote> <p>The type arguments for method <code>'Test.Blah&lt;T&gt;(System.Action&lt;T&gt;)'</code> cannot be inferred from the usage. Try specifying the type arguments explicitly.</p> <p>'System.Console' does not contain a definition for 'LineWrite'</p> </blockquote> <p>Now <em>that</em> is unexpected. Type inference is still failing (I assume because the lambda -> <code>Action&lt;T&gt;</code> conversion is failing, thus negating the compiler's guess that <code>T</code> is <code>int</code>) <em>and</em> reporting the cause of the failure.</p> <p><strong>TL; DR</strong>: I'll be glad when Eric Lippert gets around to looking at the heuristics for these more complex cases.</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