I have this conditional
if (item?.Value2?.GetType() != typeof(string) && item.get_Value() == 0)
I believe that if item is null the ?.
operation will return null, which I believe will be resolved as false
causing the condition to short circuit and all will be well (item.get_Value()
won't be called)
However I am not certain, I thought maybe I need to do it like so
if (item?.Value2?.GetType() ?? 0 != typeof(string) && item.get_Value() == 0)
but I think that might be overkill, is the first way safe from potential null reference exception?
item?.Value2?.GetType()
will return null
if item
is null
or Value2
is null
.
The condition evaluated will be
if (null != typeof(string) && item.get_Value() == 0)
so the first condition will be resolved as true
causing a NullReferenceException
when item.get_Value() == 0
will be executed (but only when item
is null
, and not Value2
)