#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.

1176-001

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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

  1. Pingback: Dew Drop – September 5, 2014 (#1849) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: