为什么这个c#代码会抛出一个空异常?
bool boolResult = SomeClass?.NullableProperty.ItsOkProperty ?? false;当NullableProperty计算为null时,elvis操作符不应该停止评估(短路)吗?
据我理解,上面的代码行是实现以下目的的快捷方式:
bool boolResult
if(SomeClass != null)
if(SomeClass.NullableProperty != null)
boolResult = SomeClass.NullableProperty.ItsOkProperty;
else
boolResult = false;
else
boolResult = false;我是不是想错了?
编辑:现在我明白了为什么我错了,代码行实际上是类似于:
bool boolResult
if(SomeClass != null)
boolResult = SomeClass.NullableProperty.ItsOkProperty;
else
boolResult = false;并抛出因为NullableProperty为空..。
发布于 2015-06-10 19:08:25
您需要链接,因为NRE位于第二个引用上:
bool boolResult = SomeClass?.NullableProperty?.ItsOkProperty ?? false;https://stackoverflow.com/questions/30765145
复制相似问题