See thread title. Can I safely do something like that without worrying about a NullReferenceException
, or is not guaranteed that those boolean expressions will be evaluated left to right?
// can this throw an NRE?
if (obj == null || obj.property == value)
{
// do something
}
To check if an object's properties have a value of null : Use the Object. values() method to get an array of the object's values.
NULL checks in C# v.The question mark symbol which used in if condition, which means that it'll check whether value is NULL, if not then it'll check whether Name is null. Also, we can also do Null using Null Coalescing operator, Var test = value ?? “value is null”; C# Copy.
They will be evaluated left to right, guaranteed. So yes, its safe.
The conditional-OR operator (||) performs a logical-OR of its bool operands, but only evaluates its second operand if necessary.
http://msdn.microsoft.com/en-us/library/6373h346%28VS.71%29.aspx
That's perfectly safe to do. If the first expression on the left is true, then the rest isn't evaluated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With