Inline If statement - short-circuiting
Asked Answered
D

6

5

As I understand and read you can use short circuiting in if statement (&& or ||) in order for second condition not to fire. and if you want both condition to fire you would use single operands (& or |).

So say if I have inline if statement as below :

var test = (MyObject != null || string.IsNullOrEmpty(MyObject.Property)) ? string.Empty : MyObject.Property;

This will throw object reference error if MyObject is null, which in my opinion should not as I am using short circuiting. Can someone please explain this.

Dihydric answered 1/3, 2013 at 11:57 Comment(2)
You're missing a closing parenthesis before the '?'.Branham
@Branham yes missed the last bracket...edited nowDihydric
S
13

You're using the wrong condition. This part:

MyObject != null || string.IsNullOrEmpty(MyObject.Property)

should be:

MyObject == null || string.IsNullOrEmpty(MyObject.Property)

The RHS of an || only executes if the left hand is false. You want it to only execute if MyObject is not null.

EDIT: If you really want the MyObject != null part, you could change the whole thing to:

var test = MyObject != null && !string.IsNullOrEmpty(MyObject.Property)
       ? MyObject.Property : "";

Note the reversal of the 2nd and 3rd operands of the conditional operator too though.

Schug answered 1/3, 2013 at 11:58 Comment(2)
ah i see so if I do && for same condition instead of || it should be fineDihydric
@Sam1: Well you'd need to reverse other bits of it as well.Schug
E
3

You should have an == not an !=

var test = (MyObject == null || string.IsNullOrEmpty(MyObject.Property) ? string.Empty : MyObject.Property
Eruct answered 1/3, 2013 at 11:59 Comment(0)
I
2

Try this:

var test = (MyObject == null || string.IsNullOrEmpty(MyObject.Property)
             ? string.Empty : MyObject.Property
Irredentist answered 1/3, 2013 at 11:59 Comment(0)
D
2
MyObject != null || string.IsNullOrEmpty(MyObject.Property) 

Here you say.

If my object is not null. or string.IsNullOrEmpty(MyObject.Property)

Which means that if MyObject is null he will try to execute the second part.

MyObject == null || string.IsNullOrEmpty(MyObject.Property)

This won't throw null exception

Dressing answered 1/3, 2013 at 12:0 Comment(0)
B
2

That happens because MyObject is null and therefore the first condition is false so the second part must be evaluated to know the whole of the condition. Change the line to this:

MyObject != null && string.IsNullOrEmpty(MyObject.Property)
Bennett answered 1/3, 2013 at 12:0 Comment(0)
E
1

You should prefer readability instead of line-count, e.g.:

string prop = string.Empty;
if(MyObject != null && MyObject.Property != null)
    prop = MyObject.Property;

(the reason for your exception was already explained in other answers)

Eroto answered 1/3, 2013 at 12:8 Comment(1)
line count isn't always the goal. For example, within a Lamda/predicate, forcing the use of { & } and also an explicit return can be just as unreadable.Foin

© 2022 - 2024 — McMap. All rights reserved.