Suppose I want to check a bunch of objects to make sure none is null:
if (obj != null &&
obj.Parameters != null &&
obj.Parameters.UserSettings != null) {
// do something with obj.Parameters.UserSettings
}
It is an alluring prospect to write a helper function to accept a variable number of arguments and simplify this kind of check:
static bool NoNulls(params object[] objects) {
for (int i = 0; i < objects.Length; i++)
if (objects[i] == null) return false;
return true;
}
Then the above code could become:
if (NoNulls(obj, obj.Parameters, obj.Parameters.UserSettings)) {
// do something
}
Right? Wrong. If obj
is null, then I'll get a NullReferenceException
when I try to pass obj.Parameters
to NoNulls
.
So the above approach is clearly misguided. But the if
statement using the &&
operator works just fine since it is short-circuited. So: is there any way to make a method short-circuited, so that its arguments are not evaluated until explicitly referenced within the method?