title: Cannot convert type 'Task<Derived>' to 'Task<Interface>'
body: <p>I have the following function with a delegate parameter that accepts a type of one interface and returns a task of another.</p>
  
  <pre><code>public void Bar(Func&lt;IMessage, Task&lt;IResult&gt;&gt; func)
  {
      throw new NotImplementedException();
  }
  </code></pre>
  
  <p>I also have a function with a parameter as an instance of <code>IMessage</code> and returns a Task. <code>Message</code> and <code>Result</code> are implementations of <code>IMessage</code> and <code>IResult</code> respectively.</p>
  
  <pre><code>private Task&lt;Result&gt; DoSomething(Message m) { return new Task&lt;Result&gt;(() =&gt; new Result()); }
  </code></pre>
  
  <p>I receive an error when I pass DoSomething into Bar.</p>
  
  <pre><code>Bar(m =&gt; DoSomething((Message)m));
  // Cannot convert type 'Task&lt;Result&gt;' to 'Task&lt;IResult&gt;'
  </code></pre>
  
  <p>Why won't <code>Result</code> implicitly convert into <code>IResult</code>? </p>
  
  <p>I would imagine it's an issue with covariance. However, in this case, <code>Result</code> implements <code>IResult</code>. I've also tried to solve the covariance issue by creating an interface and marking <code>TResult</code> as covariant.</p>
  
  <pre><code>public interface IFoo&lt;TMessage, out TResult&gt;
  {
      void Bar(Func&lt;TMessage, Task&lt;TResult&gt;&gt; func);
  }
  </code></pre>
  
  <p>But I get the error:</p>
  
  <blockquote>
    <p>Invalid variance: The type parameter 'TResult' must be invariantly
    valid on <code>IFoo&lt;TMessage, TResult&gt;.Bar(Func&lt;TMessage, Task&lt;TResult&gt;&gt;)</code>.
    'TResult' is covariant.</p>
  </blockquote>
  
  <p>Now I'm stuck. I know I have an issue with covariance but I'm not sure how to solve it. Any ideas?</p>
  
  <p>Edit: This question is specific to Tasks. I ran into this problem by implementing <code>async await</code> in my application. I came across this generic implementation and added a <code>Task</code>. Others may have the same issues during this type of conversion.</p>
  
  <p>Solution: Here's the solution based on the answers below:</p>
  
  <pre><code>Func&lt;Task&lt;Result&gt;, Task&lt;IResult&gt;&gt; convert = async m =&gt; await m;
  Bar(m =&gt; convert(DoSomething((Message)m)));
  </code></pre>