#1,176 – How an Expression Tree is Stored in Memory

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.


#1,175 – An Example of an Expression Tree

You can assign a lambda expression to an instance of an expression tree so that you then have the corresponding expression represented as a data structure.  Below is an example of how an expression is represented in an expression tree.

Suppose that we have the expression for converting Fahrenheit to Celsius, as:


We create an expression tree by decomposing the expression into a left side, operator and right side.  We then further decompose for any sub-expression that can be decomposed.  The resulting tree looks as follows:


We can interpret the expression tree as follows:

  • Main expression: (f – 32) * 5 / 9
    • Left: (f – 32) * 5
      • Left: (f – 32)
        • Left: f
        • Operator: –
        • Right: 32
      • Operator: *
      • Right: 5
    • Operator: /
    • Right: 9


#1,174 – Assigning a Lambda Expression to an Expression Tree

You can assign a lambda expression to either an instance of a delegate type or to an expression tree based on a compatible delegate type.  An expression tree is just an in-memory representation of an expression, encoding the expression as a tree.  This allows you to interact with the expression as a data structure.

            // Assign to delegate type
            Func<int, int> doubleMe = (i) => 2 * i;

            // Assign to expression tree
            // (using System.Linq.Expressions)
            Expression<Func<int, int>> doubleMeExpr = (i) => 2 * i;