CA1849: Call async methods when in an async method

Value
Rule ID CA1849
Category Performance
Fix is breaking or non-breaking Non-breaking

Cause

All methods where an Async-suffixed equivalent exists will produce this warning when called from a Task-returning method. In addition, calling Task.Wait(), Task<T>.Result, or Task.GetAwaiter().GetResult() will produce this warning.

Rule description

In a method which is already asynchronous, calls to other methods should be to their async versions, where they exist.

How to fix violations

Violation:

Task DoAsync()
{
    file.Read(buffer, 0, 10);
}

Fix:

Await the async version of the method:

async Task DoAsync()
{
    await file.ReadAsync(buffer, 0, 10);
}

When to suppress warnings

It's safe to suppress a warning from this rule in the case where there are two separate code paths for sync and async code, using an if condition. Also if there is a check for whether the Task has resolved, it is safe to use sync methods and properties.

See also