I have the following objects:
class Parent
{
public Child Child { get; set; }
}
class Child
{
public string Type { get; set; }
public int Value { get; set; }
}
Why does the following code throw a NullReferenceException?
var value = parent.Child != null &&
parent.Child.Type == "test"
? parent.Child.Value == 1
: parent.Child.Value == 2;
I would think the null-check would block the second part of the conditional from executing. However the following code works:
var value = (parent.Child != null) &&
(parent.Child.Type == "test"
? parent.Child.Value == 1
: parent.Child.Value == 2);
So I think some operator precedence is at fault here...