1

I'm confused on which expression trees need to be added to a blockexpression when wanting code to be executed.

For example, if I have ConditionalExpression, that has a reference to an IsTrue and IsFalse. Do I need to include the expressions that are for IsTrue and IsFalse. or just the Conditional Expression?

Is there a guideline, i.e. how do the IEnumerable of Expressions get executed? Do they get executed in the order they appear in the IEnumerable?

halivingston
  • 3,727
  • 4
  • 29
  • 42

1 Answers1

2

Expression trees work very similarly to C# code: there all the statements inside a block (delimited by {}) are executed in order. So, that's exactly how Expression.Block() is evaluated too: all the expressions in its Expressions collection are evaluated in order. If you want to have a ConditionalExpression inside the block, then you shouldn't put them directly into the block (unless you also want them evaluated separately, outside of the ConditionalExpression).

svick
  • 236,525
  • 50
  • 385
  • 514
  • Almost every example I see, they take an Expression, like Expression.New(someType); and then they put it in a lambda, like Expression> ... why is that? Is Expression.New not runnable by itself? – halivingston Mar 20 '14 at 06:31
  • @user986697 No, it's not, but that's very similar to C#: the expression `new someType()` is not a function you can run by itself, but `() => new someType()` is. – svick Mar 20 '14 at 12:27
  • A `BlockExpression` is also a variable scope, and is the only expression type that lets you declare local variables that can be referenced within the block (or a sub-expression of the block). – Mike Strobel Mar 20 '14 at 16:59