C# handles both nested and chained expressions, obviously. If the nesting and/or chaining is linear then it's evident what order the expressions are evaluated in:
Foo(Bar(Baz().Bop())) can only evaluate in the following order:
Baz()Bop()Bar()Foo()
But what if the nesting isn't linear? Consider: Foo(Baz()).Bar(Bop())
Clearly the following MUST all be true:
BazbeforeFooFoobeforeBarBopbeforeBar
But it's not clear exactly when Bop will be evaluated.
Any of the following would be a viable order:
- Possibility #1
Bop()Baz()Foo()Bar()
- Possibility #2
Baz()Bop()Foo()Bar()
- Possibility #3
Baz()Foo()Bop()Bar()
My instinct is that the 3rd option is likely correct. i.e. that it will fully evaluate Foo(Baz()) before it starts to evaluate any of .Bar(Bop())
Whilst I could certainly test an individual situation to see what happens, that doesn't tell me whether my guess will always be true?
But my question is: Is the order of evaluation of branched nested expressions defined as part of the C# language specification, or left to the situational judgement of the compiler?
If not, is it at least known to be deterministic?