standards-c-sharp

Avoid unnecessary use of async modifier

async does not come for free, so only add the modifier when await is necessary.

Consider the following service:

public interface IService
{
    Task<int> DoWorkAsync(int input);
}

Don’t


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); 
}

Do

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); 
}

Further reading