2

If I do this

while((e = data.IndexOf('}', at)) >= 0
      &&
      (s = data.LastIndexOf('{', at, e-1)) >= 0)

in C#, will the value of e in the second expression already have the value assigned within the while statement?

3 Answers 3

3

You can always try it out.

Short answer: Yes

Long answer: I rather create easier to read code because that kind of programming does not optimize anything. It might be opinion-based but i strongly believe it's a best practice and provides better code maintenance.

while(e = data.IndexOf('}', at)) >= 0)
{
  s = data.LastIndexOf('{', at, e-1))
  if(s < 0)
    break;
  //do stuff
}
Sign up to request clarification or add additional context in comments.

1 Comment

Surely you mean less/easier code maintenance with your example, not higher:)
0

According to the spec, http://msdn.microsoft.com/en-us/library/Aa691322, evaluation is left to right:

For example, in F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old value of i, and, finally, method H is called with the new value of i.

So yes, e will be assigned to the result of data.IndexOf('}', at), then data.LastIndexOf('{', at, e-1) will use that 'new' e.

Comments

0

Yes, because you've used && operator, which evaluates left side first to check if evaluation of the right side is necessary.

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.

from && Operator (C# Reference)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.