Ily*_*dik 13 c# c#-8.0 nullable-reference-types
在我看来,真的不能保证不可为空的变量永远不会为空。想象一下,我有一个类,它有一个不可为空的属性:
public class Foo
{
public Foo(string test)
{
Test = test;
}
public string Test {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
现在看起来它现在不能为空。但是如果我们用另一个不使用可为空上下文的库来引用这个类,没有什么可以阻止它在那里发送空值。
这是正确的还是有一些运行时检查也许可以确保这一点?
Dmi*_*soy 11
这就是 MS 所说的(https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/upgrade-to-nullable-references#interfaces-with-external-code):
编译器无法验证对公共 API 的所有调用,即使您的代码是在启用可为空注释上下文的情况下编译的。此外,尚未选择使用可为空引用类型的项目可能会使用您的库。验证公共 API 的输入,即使您已将它们声明为不可为空的类型。
| 归档时间: |
|
| 查看次数: |
1350 次 |
| 最近记录: |