Task<IList<>> as a result of Func<>

c#, generics, task-parallel-library

Solution

The issue is that `Task<T>` is not covariant (and cannot be, since it is not an interface).

So when you write `Task.FromResult(new List<Dictionary<string, object>>())`, the return type of `FromResult` is `Task<List<Dictionary<string, object>>>`, which cannot be converted to `Task<IList<Dictionary<string, object>>>`.

The solution is to specify the `FromResult` generic arguments explicitly:

var ret = Execute(() => Task.FormResult<IList<Dictionary<string, object>>>(
    new List<Dictionary<string, object>>());

Problem

I've tried to made wrap method for calling my async methods with some additional calculation: ``` private async Task<IEnumerable<Dictionary<string, object>>> Execute(Func<Task<IList<Dictionary<string, object>>>> func) { var ret = await func(); // Skipped return ret; } ``` it works perfectly for some cases: ``` var data = await Execute(() => chain.Start(parameters), args); ``` where chain.Start is ``` async Task<IList<Dictionary<string, object>>> Start(Dictionary<string, object> parameters) ``` but in some cases my func is not awaitable, so I've tried to return Task.FromResult in it. ``` var ret = Execute(() => Task.FormResult(new List<Dictionary<string, object>>()); ``` and compilator said, that Error 24 Cannot convert lambda expression to delegate type 'System.Func>>>' because some of the return types in the block are not implicitly convertible to the delegate return type Is this possible to use awaitable in this case? Or I should rewrite my code in sync manner?

Original source