I have such code:
if (a() && b != null) {
b.doSomething();
}
I need side effect of a() even if b is null. Is it guaranteed by C#? Or C# may omit a() call if b is null?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes,
a()will always be evaluated.Since the condition is evaluated from left to right,
a()will always be evaluated, butb != nullwill only be evaluated ifa()returnstrue.Here’s an exact specification reference for you, from the C# Language Specification version 3.0. My emphases and elisions.