I'm trying to return a specifically typed value from a generic function (GenericGetter).
When I try to simply return the typed Task result from GenericGetter, the compiler is showing me the following error:
Cannot convert expression type 'System.Threading.Tasks.Task<Example.ChildClass>'
to return type 'System.Threading.Tasks.Task<Example.BaseClass>'
However, if I make the function that contains the GenericGetter call async, and I return the awaited value instead, the compiler doesn't mind. It compiles, it works, but to me it seems the added async / await are redundant.
Why does GetChildClass not compile, while GetChildClassAsync does?
Here's my example:
namespace Example
{
public class BaseClass {}
public class ChildClass : BaseClass {}
public class MyExample
{
private async Task Main()
{
var foo = await GetChildClass().ConfigureAwait(false);
var bar = await GetChildClassAsync().ConfigureAwait(false);
}
private Task<BaseClass> GetChildClass() =>
GenericGetter<ChildClass>();
private async Task<BaseClass> GetChildClassAsync() =>
await GenericGetter<ChildClass>().ConfigureAwait(false);
private Task<T> GenericGetter<T>()
where T : BaseClass =>
Task.FromResult<T>(null);
}
}