" methods should not return null."> Non Async Task Null - Offensive 360 Knowledge base
Skip to content

Non Async Task Null

Non-async "Task/Task" methods should not return null

What does this mean ?

Non-async "Task/Task" methods should not return null. Returning null from a non-async Task/Task method will cause a NullReferenceException at runtime.

What can happen ?

It will cause a NullReferenceException at runtime

Recommendation

This problem can be avoided by returning Task.FromResult(null) instead.

Sample Code

Vulnerable :

public Task<object> GetFooAsync()
{
    return null; // Noncompliant
}

Non Vulnerable :

public Task<object> GetFooAsync()
{
    return Task.FromResult<object>(null);
}

References