#1,176 – How an Expression Tree is Stored in Memory
September 5, 2014 1 Comment
You can assign a lambda expression to an expression tree, storing the expression in an instance of Expression<TDelegate> (e.g. Expression<Func<double,double>>).
Suppose that we store the expression to convert from fahrenheit to celsius:
// Assign to expression tree Expression<Func<double, double>> fahrToCelsius = (f) => (f - 32.0) * 5.0 / 9.0;
We can now look at this element in memory, to see how this expression is stored. Expression<T> has a Body property that is of type Expression and represents the top-level of the expression tree. The Expression’s NodeType property indicates the operator used in the expression. In this example, the Body will be of type BinaryExpression, which has Left and Right properties which are also of type Expression and represent left and right sub-expressions.
This is shown below by examining the expression tree in the debugger for the fahrenheit to celsius expression shown above.