Confused about behavior of ?. operator

折月煮酒 提交于 2019-12-01 22:07:21

This is correct, the rest of the chain doesn't execute, null-coalescing operator operator ?? works and returns true. Per MSDN

The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result.

If you want to compare the result with either true or false (per your update) you can use

if (employee?.Address?.IsAppartment == true)
{
}

The left-hand operand returns Nullable<bool>, you can also read about it in MSND

UPDATE What will be equivalent code for following using short cut operators?
if (employee.Address != null && ? employee.Address.IsAppartment == true)

if (employee?.Address?.IsAppartment == true)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!