C#:减少完整性检查中的线路噪音[重复]

问题描述

||                                                                                                                   这个问题已经在这里有了答案:                                                      

解决方法

        这是一个相当常用的功能,因此,现在在StackOverflow上已多次询问此问题。看到 “如果对象为null则为null,如果对象不为null则为object.member”的快捷方式 对于它的一些想法。     ,        好问题,但坏意图。您的代码将忽略Demeter定律     ,        我通常只是将if语句中的表达式移到文件底部的自己的方法中:
if (CheckBat(foo))
{
    var bat = foo.bar.baz.bat;
    bat.cat = 5;
    bat.dog = 6;
}

... snip ...

private bool CheckBat(MyFoo foo)
{
    return foo != null &&
           foo.bar != null &&
           foo.bar.baz != null &&
           foo.bar.baz.bat != null;
}
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...