async
modifierasync
does not come for free, so only add the modifier when await
is necessary.
async
causes the compiler to generate a state-machine helper class with a bunch of boilerplate.Consider the following service:
public interface IService
{
Task<int> DoWorkAsync(int input);
}
private IService _service;
public async Task<int> MyMethodAsync(int input)
{
// ...
// unnecessarily wraps the already asynchronous call in a new async context
return await _service.DoWorkAsync(input);
}
private IService _service;
public Task<int> MyMethodAsync(int input)
{
// ...
// service already returns Task<int>, so just return the result directly, and remove the async modifier
return _service.DoWorkAsync(input);
}