Returning null
from a non-async
Task
/Task<TResult>
method will cause a
NullReferenceException
at runtime if the method is awaited. This problem can be avoided by returning Task.CompletedTask
or Task.FromResult<TResult>(null)
respectively.
public Task DoFooAsync()
{
return null; // Noncompliant: Causes a NullReferenceException if awaited.
}
public async Task Main()
{
await DoFooAsync(); // NullReferenceException
}