Short circuit in C # 6 Elvis (short circuit)
Why is this C # code throwing an exceptional exception?
bool boolResult = SomeClass?.NullableProperty.ItsOkProperty ?? false;
isn't the truth of the Elvis operator supposed to stop evaluating (short-circuiting) after NullableProperty evaluates to null?
In my understanding, the above line of code is a shortcut to:
bool boolResult
if(SomeClass != null)
if(SomeClass.NullableProperty != null)
boolResult = SomeClass.NullableProperty.ItsOkProperty;
else
boolResult = false;
else
boolResult = false;
I was wrong?
EDIT: Now I understand why this is wrong, the line of code is actually translating to something similar to:
bool boolResult
if(SomeClass != null)
boolResult = SomeClass.NullableProperty.ItsOkProperty;
else
boolResult = false;
And it throws because NullableProperty is null ...
+3
source to share