2

I am refractoring a line of code from .NET 4.0 to 3.5, the actual code is

if(bar?.foo == "outdated")
    bar = new Bar();

Can i refractor these lines in this way and retain the same logic?

if(bar == null || bar.foo == "outdated")
    bar = new Bar();

Does the if evaluation stop after the the bar == null (since the expression would be true, no matter whether the other part returns true or false) or does it continue to check bar.foo even if the first part already returned true, and therefore throw a null exception?

Jacopo
  • 525
  • 3
  • 15
  • Offtopic: why are you going to a lower version on .NET? (just interest) – RvdK Jan 10 '17 at 09:47
  • 2
    Could have been answered with a look at the documentation: https://msdn.microsoft.com/en-us/library/6373h346.aspx – Tim Schmelter Jan 10 '17 at 09:47
  • The answer is **yes**, the if evaluation stops after the the bar == null . That's the difference between `|` and `||` , while `|` will continue compiling even when the first section of the condition returns true. check [this](https://msdn.microsoft.com/en-us/library/6373h346.aspx) out for more information. – Null Jan 10 '17 at 09:51
  • @RvdK i was creating a plug-in for our software, but it lately turned out that this plug-in was also required for an older version – Jacopo Jan 10 '17 at 09:54
  • @Null you are right, i edited the example to better reflect what i'm actually doing (Same for Hans Kesting) – Jacopo Jan 10 '17 at 09:57

1 Answers1

5

Yes it stops, it is called short circuit logic, Check this link : https://msdn.microsoft.com/en-us/library/6373h346.aspx

Ali Ezzat Odeh
  • 2,093
  • 1
  • 17
  • 17