You might know this already, but in C# whenever you write something like this:
it’s not guaranteed that both methods will be executed, so DO NOT DO THIS if you’re relying on both methods to be expected to cause some desirable side-effects.
The reason for this is simple, at runtime, as soon as one of the methods returns true the whole expression will evaluate to true regardless of the output of the second method. So as far as the runtime is concerned, it can safely skip the second part of the if condition as a form of runtime optimization.
Here’s a quick demo that shows this behaviour in action:
This behaviour also applies outside to if statements like the one at the top of the post. I wasted some valuable minutes trying to solve a WTF bug resulting from this, hopefully it won’t catch you out too!
Subscribe to my newsletter and get new contents delivered straight to your inbox :-)