如果为null,null条件运算符是否返回false?

hel*_*ale -5 c# null-coalescing-operator nullreferenceexception c#-6.0

我有条件的

if (item?.Value2?.GetType() != typeof(string) && item.get_Value() == 0)

我相信如果item为null,?.操作将返回null,我认为这将解决为false导致条件短路并且一切都会很好(item.get_Value()不会被调用)

但是我不确定,我想也许我需要这样做

if (item?.Value2?.GetType() ?? 0 != typeof(string) && item.get_Value() == 0)

但我认为这可能是矫枉过正,是否可以安全地避免潜在的空引用异常?

Alb*_*rto 5

item?.Value2?.GetType()将返回null如果itemnull或者Value2null.

评估的条件是

if (null != typeof(string) && item.get_Value() == 0)
Run Code Online (Sandbox Code Playgroud)

所以第一个条件将被解决为true导致NullReferenceException何时item.get_Value() == 0执行(但只有当itemnull,而不是Value2)