• Advertisement
Sign in to follow this  

[.net] C# expression evaluation

This topic is 4251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Are standard C# compilers required to evaluate conditional expressions within an "if" from left to right? For example, is it possible for the following code segment to throw a NullReferenceException (because of the .ToString() call)? object x = null; if ( (x != null) && (x.ToString() == "string") ) { // do something }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
No, it's not possible.

Share this post


Link to post
Share on other sites
It has nothing to do with the if, and everything to do with the &&. The rules for short-circuiting && say that the left operand is evaluated first, and that if it evaluates to false the right operand is not evaluated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement