I have the following OR operator, now currently if the c is null the Error view will be returned and no null exception will be raised on the second check (!c.ManagedBy…); but this will only happen if the compiler perform the (c==null) check before the (!c.Ismanaged..) check.
Book c = elearningrepository.GetBook(id);
if ((c == null) || (!c.IsManagedBy(User.Identity.Name)))
{
return View("Error");
}
so can I guarantee that the compiler will always perform the c==null check first or it is better to split the above check into two separate If statements to explicitly enforce the sequence.
BR
||does short-circuit and will evaluate exactly as you have described. There’s no need to separate the statements.From MSDN:
Edited for the most recent MSDN article (2010).