我开始在 VS19 预览版中收到这些警告
<TargetFramework>net5.0</TargetFramework>
<LangVersion>preview</LangVersion>`
<Nullable>enable</Nullable>
下面是一个类的示例,该类将其许多方法代理到 ImmutableList<T>
:
class C<T> {
readonly ImmutableList<T> composed;
public C() => composed = ImmutableList<T>.Empty;
...
public T Find(Predicate<T> match) => composed.Find(match);
...
}
warning CS8603: Possible null reference return. for the
Find
method
我不明白为什么,因为它与 ImmutableList<T>.Find
具有相同的签名?
解决这个问题的最佳方法是什么?
最佳答案
the docs 中对此进行了描述
Returns
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.
因此返回 T?
或者使用 ??
返回非空值。
关于相同的签名,在代码中您可以看到:
[return: MaybeNull]
public T Find(Predicate<T> match)
Specifies that an output may be null even if the corresponding type disallows it.
关于c# - 了解可能的空引用返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63145379/