I read that the AND && operator in .NET conditionally evaluates its second argument.
Is it safe to check for null and then check for any field inside the same statement?
For example, imagine that we have a class SomeClass that has an integer field Id:
class SomeClass { public Int32 Id { get; set; } }
Then we get an object of this type somewhere and try to perform some actions on it:
public void SomeMethod(SomeClass obj) { if (obj != null) if (obj.Id == 1) {
Is it possible to rewrite this as follows to reduce the amount of code? Will checking for zero be safe?
public void SomeMethod(SomeClass obj) { if (obj != null && obj.Id == 1)
source share