Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you pass a <code>Func&lt;TResult&gt;</code> into a method <code>Run&lt;TResult&gt;(Func&lt;TResult&gt;)</code> you don't have to specify the generic on the methodcall because it can infer it. Your lambda does that inference.</p> <p>However, your function is not actually a <code>Func&lt;TResult&gt;</code> whereas the lambda was.</p> <p>If you do <code>Func&lt;Int32&gt; f = MyIntReturningMethod</code> it works. Now if you specify <code>Task.Run&lt;Int32&gt;(MyIntReturningMethod)</code> you would expect it to work also. However it can't decide if it should resolve the <code>Func&lt;Task&lt;TResult&gt;&gt;</code> overload or the <code>Func&lt;TResult&gt;</code> overload, and that doesn't make much sense because its obvious that the method is not returning a task. </p> <p>If you compile something simple like follows:</p> <pre><code>void Main() { Thing(MyIntReturningMethod); } public void Thing&lt;T&gt;(Func&lt;T&gt; o) { o(); } public Int32 MyIntReturningMethod() { return (5); } </code></pre> <p>the IL looks like this....</p> <pre><code>IL_0001: ldarg.0 IL_0002: ldarg.0 IL_0003: ldftn UserQuery.MyIntReturningMethod IL_0009: newobj System.Func&lt;System.Int32&gt;..ctor IL_000E: call UserQuery.Thing </code></pre> <p>(Some of the extra stuff is from LINQ Pad's additions... like the UserQuery part)</p> <p>The IL looks identical as if you do an explicit cast. So it seems like the compiler does't actually know which method to use. So it doesn't know what cast to create automatically.</p> <p>You can just use <code>Task.Run&lt;Int32&gt;((Func&lt;Int32&gt;)MyIntReturningMethod)</code> to help it out a bit. Though I do agree that this seems like something the compiler should be able to handle. Because <code>Func&lt;Task&lt;Int32&gt;&gt;</code> is not the same as <code>Func&lt;Int32&gt;</code>, so it doesn't make sense that they would confuse the compiler.</p>
    singulars
    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. 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.
    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