在我看来,真的不能保证非空变量永远不会有null。想象一下,我有一个具有不为空的属性的类:
public class Foo
{
public Foo(string test)
{
Test = test;
}
public string Test {get;set;}
}
现在看起来好像现在不能为空。但是,如果我们用另一个不使用可为空的上下文的库引用该类,则没有什么可以阻止它在其中发送null的。
那是正确的还是有一些运行时检查也可以确保这一点?
public void Foo(string test){...}
还是public Foo(string test){...}
?