C#

C#

Made by DeepSource

Non-Async methods with return type Task/Task<T> should not return null CS-R1006

Anti-pattern
Critical

A non-async method with a return type of either Task or Task<T> should never return null. Doing so may result in a NullReferenceException at runtime. Therefore, it is suggested that you either modify your method's logic or use Task.FromResult<T>(null).

Bad Practice

public Task<object> GetDataFromServer(string endpoint)
{
    if (/* ... */)
    {
        return null;
    }

    // ...
}

Recommended

public Task<object> GetDataFromServer(string endpoint)
{
    if (/* ... */)
    {
        return Task.FromResult<object>(null);
    }

    // ...
}

Reference