class ResultBase {}
class Result : ResultBase {}
Task GetResult() {
return Task.FromResult(new Result());
}
The compiler tel
I realize I'm late to the party, but here's an extension method I've been using to account for this missing feature:
///
/// Casts the result type of the input task as if it were covariant
///
/// The original result type of the task
/// The covariant type to return
/// The target task to cast
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task AsTask(this Task task)
where T : TResult
where TResult : class
{
return await task;
}
This way you can just do:
class ResultBase {}
class Result : ResultBase {}
Task GetResultAsync() => ...; // Some async code that returns Result
Task GetResultBaseAsync()
{
return GetResultAsync().AsTask();
}