(By request, promoted from a comment.)
This is by design of the async
logic in the C# Language Specification. See the section Async Functions.
You can also read the programming guide, Async return types (C#) § Task<TResult>.
The point is that a method without the async
keyword, like
public Task<int> HandleAsync()
{
...
Task<int> result = ...;
...
return result;
}
must return what it says it returns, just as with all other types. In this case, you could create a Task<>
that can be awaited, in your own manual way, like:
Task<int> result = Task.Run(GetLastDigitOfSmallestTwinPrimeWithMoreThan1000000Digits);
The caller can await
the task you return, even if your method is written like above.
But if you use the async
keyword, and if your return type is, say, Task<int>
, then you must return a plain int
, and the C# compiler will write the needed logic for you.
Usually, you would use await
somewhere in the body (otherwise, why async
). So something like:
public async Task<int> HandleAsync()
{
...
int result = await GetLastDigitOfSmallestTwinPrimeWithMoreThan1000000DigitsAsync();
...
return result;
}
Related question here: Why is it not necessary to return a Task when the signature is public async Task MethodName?