ReSharper 代码注释异步任务<T>

ReSharper Code Annotations async Task<T>

是否可以标记async Task<T>的结果可以为空?使用属性 [CanBeNull] 不起作用,因为异步任务的 return 值永远不会为空。

[CanBeNull] // not working...
private async Task<T> doSomeFancyAsyncStuff([NotNull] object icantbenull) { ...

您可以使用 Resharper ItemCanBeNullAttribute

Can be appplied to symbols of types derived from IEnumerable as well as to symbols of Task and Lazy classes to indicate that the value of a collection item, of the Task.Result property or of the Lazy.Value property can be null.

ItemCanBeNullAttribute

应用于您的示例:

[ItemCanBeNull]
public async Task<string> GetSomeName() {
    var time = DateTime.Now;
    if(time.Second == 30) { 
        return "Jimmy"; 
    } else {
        return null;
    }
}