I read that the AND operator && in .NET conditionally evaluates its second argument.
Is it safe to check for null and then check some field within a single statement?
For example, imagine we have a class SomeClass, which has an integer field Id:
class SomeClass
{
public Int32 Id { get; set; }
}
Then, we receive 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)
{
// do some actions
}
}
Can we rewrite this as follows to reduce the amount of code? Will the null-check be safe?
public void SomeMethod(SomeClass obj)
{
if (obj != null && obj.Id == 1)
// do some actions
}