0

Possible Duplicate:
Is relying on && short-circuiting safe in .NET?

I wasn't sure how to phrase the title of the question correctly, but it's very simple to explain with a single line of code:

if (someObject == null || someObject.someProperty)
    ...

Can I do that? Or this one:

if (someObject != null && someObject.someProperty)
    ...
Community
  • 1
  • 1
  • 2
    Have you tried it? You ca translate these operators with (VB.NET style): `OrElse` + `AndAlso` which is more meaningful. – Tim Schmelter Jan 13 '13 at 21:56
  • @TimSchmelter Not sure what you know by "more meaningful." If you're working in C#, you should understand about short circuiting and its behavior, just as a VB.Net programmer should know about the difference between `And` and `AndAlso`. – Andy Jan 13 '13 at 22:00
  • @Andy: Since OP didn't know the operators and what they do, i've used the VB.NET equivalent to emphasise their meaning (imho `Also` is more meaningfu than another `&`). – Tim Schmelter Jan 13 '13 at 22:02
  • @TimSchmelter I have no issue with pointing out the equivalent in VB.Net; it's the "more meaningful" part. Its you're opinion, but in my experience most C# developers fully understand `&&` and `||` short circuit, and for us its perfectly clear, especially since `&` and `|` are normally only used when bit-wise operators are wanted. – Andy Jan 13 '13 at 22:13
  • 1
    @Andy: yes, but obviously not for OP, otherwise this question would be pointless ;) – Tim Schmelter Jan 13 '13 at 22:14

5 Answers5

5

Yes.

The second operand will only be evaluated if the first operand is false.

Obviously, though, the second operand must also be a boolean.

Similarly, && will only evaluate the second operand if the first operand is true.

This is called short-circuiting.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I also added the second part of the question, but you was lightning-fast with an answer... I think that it's supposed to similar though, isn't it? – Max Yankov Jan 13 '13 at 21:57
4

Yes, this is safe. || and && are short-circuiting operators. From the MSDN Library:

The conditional-OR operator (||) performs a logical-OR of its bool operands, but only evaluates its second operand if necessary.

The operation

x || y

corresponds to the operation

x | y

except that if x is true, y is not evaluated (because the result of the OR operation is true no matter what the value of y might be). This is known as "short-circuit" evaluation.

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

Yes, it is common practice to use null-checking in an if statement, usually with an AND.

This is valid because the statement will short-circuit and end if the first is true.

Both these are valid:

if(something != null && something.Bool)
  ...

if(something != null || something.Bool)
  ...
CC Inc
  • 5,842
  • 3
  • 33
  • 64
0

Yes, || and && both short-circuit. Indeed, you occasionally see people writing code like this:

x != null && x.f();

Which is equivalent to:

if(x != null) x.f();

(Probably not a good idea most of the time - it's not good for readability.)

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
0

Yes you can, but which one you want depends on the semantics you're after.

In the first case, your condition will operation if there's no object at all or if the property returns true. Basically the absence of an instance implies the conditional logic should execute.

In the second case, the absence of an instance will NOT cause your conditional to run; in this case, you must both have an instance and the property must evaluate to true.

I dealt with this very same thing today when loading user preferences. I had to decide if the preference was "checked" or not, and since we may add new preferences (without adding rows for each user for that preference to the database), I chose the latter condition as that met my use case.

Andy
  • 8,432
  • 6
  • 38
  • 76