Updated on

An expression tree in C# is code stored as data: a tree of objects from System.Linq.Expressions where each node is a parameter, a constant, an operator, a method call or another expression. We can read that tree, translate it into SQL, or compile it into a delegate and run it.

Although the topic may initially sound complex, it’s much simpler when we know a few concepts around it. We’ll build a tree two ways, take one apart node by node, see where Entity Framework, dynamic methods and Moq use them, and measure what compiling one costs.

To download the source code for this article, you can visit our GitHub repository.

Let’s get started. 

What Are Expression Trees?

Expression trees let us represent code as data. When we assign a lambda to an Expression<TDelegate> instead of a delegate, the compiler emits code that builds a tree of objects describing the lambda. Each node in that tree represents an operation, a variable, a constant or another code element.

Microsoft Learn describes them as code “in a tree-like data structure, where each node is an expression”.

The node types live in the System.Linq.Expressions namespace and all derive from the Expression base class: ParameterExpression for a parameter, ConstantExpression for a value, BinaryExpression for an operator such as +, MethodCallExpression for a call, and so on.

Because the code is now data, we can work with it at runtime. We can compile it into a delegate and run it, walk the tree and build a changed copy, or translate it into another language such as SQL. That last one is why most of us have used expression trees without knowing it.

How Do We Create an Expression Tree?

There are multiple ways to create expression trees. The easiest way is to create directly from the lambda expressions. The other way is to use the Expression class and manually create it. Let’s have a look at both of them.

Create Expression Trees From Lambda Expression

Before proceeding, we should have a good understanding of Lambda Expressions.

Let’s create a simple lambda expression to find a sum of two numbers and later see how we can convert it to an Expression Tree:

Func<int, int, int> sum = (int number1, int number2) => number1 + number2;

The given lambda expression takes two parameters number1 and number2 of type integers and returns their sum as an integer. Hence, the inferred type of this expression will be Func<int, int, int>.

Now if we want to execute this lambda expression, we can do so by simply invoking it like a function:

sum(1, 2);

To convert the same lambda expression into an expression tree, let’s first create a new ExpressionTrees class and create a new method:

using System.Linq.Expressions;

public static Expression<Func<int, int, int>> CreateExpressionTreeFromLambdaExpression()
{
    Expression<Func<int, int, int>> sumExpressionTree = (int number1, int number2) => number1 + number2;

    return sumExpressionTree;
}

We can convert it by simply assigning it to the  Expression<TDelegate> class where TDelegate is a generic delegate type parameter.

If we look closely, the right-hand side of the statement remains unchanged. But on the left-hand side, we replaced the TDelegate generic delegate parameter of Expression<TDelegate> class with Func<int, int, int>.

We might wonder what’s the effect after making this change. Well, the compiler automatically converts the lambda expression into a tree-like data structure on assignment to the Expression<TDelegate> class.

Create Expression Tree Using Expression Tree API

Previously, we created an expression tree directly from a lambda expression. Now let’s look at how we can create it manually using the Expression class and its factory methods.

Let’s imagine the same lambda expression and build it as Expression Tree:

(int number1, int number2) => number1 + number2;

Let’s create another method:

public static Expression<Func<int, int, int>> CreateExpressionTreeUsingExpressionTreeClass()
{
    ParameterExpression param1 = Expression.Parameter(typeof(int), "number1");
    ParameterExpression param2 = Expression.Parameter(typeof(int), "number2");

    BinaryExpression sumBody = Expression.Add(param1, param2);

    Expression<Func<int, int, int>> sumExpression 
        = Expression.Lambda<Func<int, int, int>>(sumBody, param1, param2);

    return sumExpression;
}

We first create a parameter expression for each lambda expression parameter using Expression.Parameter() method.

Next, we create a binary expression using BinaryExpression and applying the Expression.Add() method because the + operator represents the addition operation.

Finally, we create a lambda expression that combines parameter and binary expressions using the Expression.Lambda() method. In our case, the lambda expression takes two integer parameters and returns their sum.

Let’s discuss compiling and executing expression trees next.

Compiling and Executing Expression Trees

We cannot directly execute an expression tree like a regular method or delegate because an expression tree object only holds the data. Using this data, we can compile it as an executable delegate.  

We need to call the Compile() method from the Expression<TDelegate> object to compile and execute an expression tree. This method returns a delegate of the type TDelegate which we can invoke with the desired arguments.

Let’s create a new method that calls the Compile() method:

public static Func<int, int, int> CompileExpressionTreeToLambdaExpression
(Expression<Func<int, int, int>> expressionTree)
{
    var delegateRepresentingLambdaExpression = expressionTree.Compile();

    return delegateRepresentingLambdaExpression;
}

In Program.cs, let’s compile the sumExpressionTree that we built earlier and execute it:

using System.Linq.Expressions;

var sumExpressionTree = ExpressionTrees.CreateExpressionTreeFromLambdaExpression();
var delegateSumExpressionTree = ExpressionTrees.CompileExpressionTreeToLambdaExpression(sumExpressionTree)(1, 2);
Console.WriteLine(delegateSumExpressionTree); // prints 3

The Compile() method generates IL code for the expression tree and creates a delegate. The runtime JITs this IL code into machine code like the rest of the program. This process can be expensive, so we should avoid compiling the same expression tree multiple times.

Compile() won’t do that for us. Calling it twice on the same tree returns two different delegates, so the caching is our job: compile once, keep the delegate in a field or a dictionary, and reuse it. We measure the difference at the end of the article.

Expression Tree Explained

To understand more about expression trees, let’s print the values of some of the properties of the sumExpressionTree object and see what value each property holds:

Console.WriteLine(sumExpressionTree.NodeType); // prints "Lambda"
Console.WriteLine(sumExpressionTree.Parameters[0].Name); // prints "number1"
Console.WriteLine(sumExpressionTree.Parameters[1].Name); // prints "number2"
Console.WriteLine(sumExpressionTree.Body.ToString()); // prints "(number1 + number2)"
Console.WriteLine(sumExpressionTree.ReturnType); // prints "System.Int32"

From the output, we can see that the sumExpressionTree.NodeType enum holds the Lambda value. This is the root node of our expression tree. The sumExpressionTree.Parameters property has two items with the values for their Name property as number1 and number2 respectively.

The value of  sumExpressionTree.Body property as (number1 + number2) and the sumExpressionTree.ReturnType as System.Int32.

This pretty much expresses the whole lambda expression as data.

Let’s look into the sumExpressionTree.Body child node and print the NodeType enum property:

Console.WriteLine(sumExpressionTree.Body.NodeType); // prints "Add"

The Add enum value in the sumExpressionTree.Body.NodeType states that we are doing an additive operation.

That said, we can assign sumExpressionTree.Body to a body variable:

BinaryExpression body = (BinaryExpression)sumExpressionTree.Body;

While assigning, we cast to BinaryExpression type to traverse further into the tree. We chose BinaryExpression type because of the additive operation that we are performing. In fact, BinaryExpression is the derived type of Expression base class.

Once we do this, we get additional properties like Left and Right to express the binary additive operation. Let’s print the NodeType of body.Left property and see what value it holds for us:

Console.WriteLine(body.Left.NodeType); // prints "Parameter"

The value of body.Left.NodeType property is Parameter enum value.

Additionally, we can assign body.Left to a leftParam variable:

ParameterExpression leftParam = (ParameterExpression)body.Left;

While assigning, we cast to ParameterExpression as the NodeType is Parameter enum value.

Now we get additional properties in leftParam object:

Console.WriteLine(leftParam.Name); // prints "number1"

From the printed output, the value of leftParam.Name is number1. This represents the parameter name we used as the left parameter to declare the lambda expression.

It’s important to note that both the leftParam and sumExpressionTree.Parameters[0] hold the same reference as they are essentially the same parameter.

Similarly, we can inspect the body.Right node and have similar inferences as body.Left:

Console.WriteLine(body.Right.NodeType); // prints "Parameter"
ParameterExpression rightParam = (ParameterExpression)body.Right;

Console.WriteLine(rightParam.Name); // prints "number2"

If we can picture this tree traversal, we find it to be a tree-like structure and hence the name Expression Tree:

Expression tree for (number1, number2) => number1 + number2: a LambdaExpression root with a Parameters collection and a BinaryExpression body; the body's Left and Right are the same two ParameterExpression nodes the Parameters collection holds.

The diagram shows that the entire lambda expression is the root node. The root node contains Parameters and Body as its child nodes. The Body node then has Left and Right parameter nodes as its child nodes, completing the tree structure. The dashed lines suggest that the Left and the Right nodes hold the same object references as the Parameters node of the root node.

Applications of Expression Trees

We often use Expression Trees in scenarios such as LINQ queries, dynamic programming, and mocking frameworks. Let’s explore some of these applications one by one.

Entity Framework

The first thing that comes to mind is Entity Framework when discussing Expression Trees. Entity Framework uses expression trees to convert lambda expressions into raw SQL queries. We can write queries in C# that can execute on a database server without writing raw SQL strings.

Before we continue with the query, we have to prepare EF Core for our project. As this article is not related to EF Core preparation, we’ve created all the necessary files that you can find in our source code.

That said, let’s open the EntityFrameworkSqlite class and inspect a simple Entity Framework LINQ Query that filters the list of users by age:

_dbContext
    .Users
    .Where(x => x.Age > 20)
    .ToList();

This query is an expression tree that represents the lambda expression x => x.Age > 20. The ORM can inspect this expression tree and translate it to a SQL query at runtime:

SELECT "u"."Id", "u"."Age", "u"."Name"
FROM "Users" AS "u"
WHERE "u"."Age" > 20

This way, we can write queries in a type-safe and readable way and let the ORM handle the database communication.

Let’s see how it works.

To proceed, we need to know the signature of the Where() method:

IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)

The Where() is the extension method of the IQueryable<TSource> interface.

The  second parameter’s type is Expression<Func<TSource, bool>> which will convert the regular lambda expression into an expression tree object. Thus, the Entity Framework database providers can inspect this expression tree object at runtime and convert it to raw SQL queries.

So what happens if we pass a Func instead?

The code still compiles, which is the dangerous part. Our EntityFrameworkSqlite class also has two methods that differ only in the predicate’s type:

public IEnumerable<User> FilterWithFunc(Func<User, bool> predicate)
{
    return _dbContext.Users.Where(predicate);
}

public IQueryable<User> FilterWithExpression(Expression<Func<User, bool>> predicate)
{
    return _dbContext.Users.Where(predicate);
}

With an Expression, the compiler picks Queryable.Where() and EF Core gets a tree it can translate. With a Func, there is no tree to read. The compiler picks Enumerable.Where() instead, EF Core sends a query with no WHERE clause, and the filter runs in our process on every row it loaded.

We can see it in the SQL EF Core logs when we add optionsBuilder.LogTo(Console.WriteLine); to OnConfiguring() in UserDbContext and enumerate both results:

-- FilterWithFunc(x => x.Age > 20)
SELECT "u"."Id", "u"."Age", "u"."Name"
FROM "Users" AS "u"

-- FilterWithExpression(x => x.Age > 20)
SELECT "u"."Id", "u"."Age", "u"."Name"
FROM "Users" AS "u"
WHERE "u"."Age" > 20

On a table with a few rows, nobody notices. On a table with a million, the first version reads all of them. This is the difference between IEnumerable and IQueryable in practice, and the parameter type decides which one we get.

Dynamic Methods

Another application of expression trees is creating dynamic methods we can compile and execute at runtime. This can be useful when generating code based on some input or configuration.

For instance, in a new public DynamicMethodGenerator class, we can dynamically interpret the math statements provided by the users and convert them to an executable code:

using System.Linq.Expressions;

public static Func<int, int, int> GenerateExecutableFunctionFromMathStatement(string mathStatement)
{
    // Parse the math statement
    var parts = mathStatement.Split('=');
    var variables = parts[1].Split('*');

    var left = Expression.Parameter(typeof(int), variables[0]);
    var right = Expression.Parameter(typeof(int), variables[1]);

    // Create the expression tree
    var product = Expression.Multiply(left, right);
    var lambda = Expression.Lambda(product, left, right);

    // Return the compiled method
    return (Func<int, int, int>)lambda.Compile();
}

This method accepts the math statement as a parameter, parses it, and converts it to an expression tree. Finally, it returns the executable function by calling the Compile() method. We need to call this method to get the function with the math statement =a*b, which we do in a test in an xUnit project that references our main project:

var productFunc = DynamicMethodGenerator.GenerateExecutableFunctionFromMathStatement("=a*b");

var product = productFunc(2, 5);

Assert.Equal(10, product);

Please note that this is a simple demonstration to accept only one math statement to do a product of two numbers. But if we get the idea, much more complex scenarios are possible. Two of those scenarios have their own articles: building dynamic LINQ queries with expression trees, and the System.Linq.Dynamic.Core library, which parses a string like "Age > 20" into an expression tree for us.

Mocking

The other popular application of expression trees is to create mock objects for unit testing. A mock object is a fake object that mimics the behavior of a real object but allows us to control its output and verify its interactions.

To demonstrate, let’s create an ICalculator interface containing one method Add() that takes two numbers as input parameters:

public interface ICalculator
{
    int Add(int number1, int number2);
}

Next, we need the Calculator class that implements this interface:

public class Calculator : ICalculator
{
    public int Add(int number1, int number2) => number1 + number2;
}

Then, let’s create a CalculatorService class that takes ICalculator as a dependency:

public class CalculatorService
{
    private readonly ICalculator _calculator;

    public CalculatorService(ICalculator calculator)
    {
        _calculator = calculator;
    }

    public int AddTwoNumbers(int number1, int number2)
    {
        return _calculator.Add(number1, number2);
    }
}

The AddTwoNumbers() method will call the Add() method of the instance of ICalculator.

In this case, we can use a mocking framework such as Moq to create a mock object for an interface. We add the Moq package to the test project first:

using Moq;

var calculatorMock = new Mock<ICalculator>();
calculatorMock.Setup(c => c.Add(1, 2)).Returns(3);

Here, we create a mock object that implements the ICalculator interface and sets up an expectation that when the Add() method is called by passing in the numbers 1 and 2 as the arguments, and it should return a total number of 3.

We can use this mock object in our unit tests to verify the logic of our code that depends on the ICalculator interface:

var calculatorService = new CalculatorService(calculatorMock.Object);

var sum = calculatorService.AddTwoNumbers(1, 2);

Assert.Equal(3, sum);

The assertion will pass because the AddTwoNumbers() method within the CalculatorService class calls the mocked Add() method with the same arguments and returns the sum that equals 3.

The mocking frameworks use expression trees to capture the methods we want to mock and the arguments we pass to them. Moq’s Setup() takes an Expression<Func<ICalculator, int>> as its parameter, so Moq never calls c.Add(1, 2). It reads the tree, finds the Add method call node and its two constant arguments, and remembers them. Verify() reads its argument the same way to check which calls the mock received.

These are just some of the applications of expression trees in C#. There are other advanced applications like the dynamic language runtime (DLR), which uses expression trees to provide interoperability between .NET and dynamic languages such as IronPython.

What Can’t an Expression Tree Contain?

An expression tree can hold only what fits in a single expression. The compiler builds a tree from an expression lambda and refuses a lambda with a statement body in braces, with error CS0834: “A lambda expression with a statement body cannot be converted to an expression tree”.

The other refusals come from the same place: the set of node types was fixed long ago, so newer C# syntax either maps to an older form or fails at compile time. On the .NET 10 SDK we get CS1989 for an async lambda, CS8188 for a throw expression, CS0838 for a multidimensional array initializer and CS9175 for a collection expression. A dynamic operation fails with CS1963, “An expression tree may not contain a dynamic operation”.

Interpolated strings compile on the .NET 10 SDK, even though Microsoft’s list still names them: the tree stores them as a string.Format() call.

For statements, we build the tree by hand. The Expression API has Block, Assign and Loop nodes.

Any powerful feature is not without its limitations. Expression trees can’t use some of these features:

  • async, await in the expression trees
  • Statements or statement-bodied lambda expressions (multi-line lambda)
  • Throw expressions
  • Multi-dimensional array initialization
  • Collection expressions such as [1, 2]
  • dynamic operations
  • and more

Microsoft Learn keeps the complete list of limitations.

How Expensive Is Compiling an Expression Tree?

Compiling a tree costs far more than running the result.

In a full BenchmarkDotNet check on .NET 10, a cached compiled delegate ran our (number1, number2) => number1 + number2 tree about as fast as the plain lambda, both under a nanosecond per call. Calling Compile() and then the delegate on every call took about 40 microseconds and allocated about 4.2 KB each time. That is tens of thousands of times slower, so the main advice below is to compile once and keep the delegate.

Compiling costs this much because it generates IL for a new dynamic method that the JIT then turns into machine code.

Compile(preferInterpretation: true) is a middle option. It skips IL generation and returns a delegate that runs the tree through an interpreter. Creating and calling it took about 1.5 microseconds in the same check, and a cached interpreted delegate ran in under 100 nanoseconds per call. That suits a tree we run only a handful of times.

Here is the benchmark class from the Benchmarks project in our source code, which reuses the sum tree and the plain lambda from the ExpressionTrees class there:

[MemoryDiagnoser]
public class CompileBenchmarks
{
    private readonly Expression<Func<int, int, int>> _sumExpressionTree =
        ExpressionTrees.CreateExpressionTreeFromLambdaExpression();

    private readonly Func<int, int, int> _lambda = ExpressionTrees.CreateLambdaExpression();
    private Func<int, int, int> _compiled = default!;
    private Func<int, int, int> _interpreted = default!;

    [GlobalSetup]
    public void Setup()
    {
        _compiled = _sumExpressionTree.Compile();
        _interpreted = _sumExpressionTree.Compile(preferInterpretation: true);
    }

    [Benchmark]
    public int InvokeLambda() => _lambda(1, 2);

    [Benchmark]
    public int InvokeCachedCompiledDelegate() => _compiled(1, 2);

    [Benchmark]
    public int InvokeCachedInterpretedDelegate() => _interpreted(1, 2);

    [Benchmark]
    public int CompileAndInvoke() => _sumExpressionTree.Compile()(1, 2);

    [Benchmark]
    public int InterpretAndInvoke() => _sumExpressionTree.Compile(preferInterpretation: true)(1, 2);
}

The first three methods measure only the call, because the delegates are built once, in a field initializer or in Setup(). The last two pay for building the delegate on every call, which is what happens when we forget to cache it.

What we do on each callMean timeAllocated
Call a plain lambda0.0074 ns-
Call a cached compiled delegate0.7864 ns-
Call a cached interpreted delegate72.3183 ns224 B
Compile(preferInterpretation: true), then call1,470.7636 ns1608 B
Compile(), then call39,452.5730 ns4335 B

To reduce the overhead of creating and compiling expression trees, we can follow these guidelines:

  • Cache the expression trees and their compiled delegates whenever possible
  • The bigger the tree grows, the higher the compute and memory required to compile
  • Use expression trees only when we need their flexibility and power

Our deep copy article ran into exactly this. Its expression-tree copier used to compile a new delegate on every call, and once the delegate was cached it went from the slowest technique in that benchmark to one of the fastest.

Conclusion

In this article, we have seen how to create and use expression trees in different scenarios. We have also learned its different applications. Finally, we also looked into its limitations and performance considerations. In practice, that comes down to two habits: pass an Expression when a library needs to read our code, and compile a tree once when we need to run it. You can find more articles in C# Intermediate to learn intermediate and advanced topics in C#.

Tested with .NET 10.0.10, Entity Framework Core 10.0.14 and Moq 4.21.0.