C# Expression Trees

Learn how to use expression trees in C# to represent code as data structures.

Introduction to C# Expression Trees

Expression trees are a powerful feature in C# that allow you to represent code as a hierarchical tree structure. This enables you to analyze, modify, and execute code at runtime. They are fundamental to technologies like LINQ (Language Integrated Query) and are invaluable for building dynamic and reflective applications.

What are Expression Trees?

An expression tree represents code as a data structure, where each node in the tree is an expression, such as a method call, a binary operation, a constant value, or a parameter. Unlike delegates, which represent executable code, expression trees represent the structure of that code.

Key Concepts

Creating Expression Trees

You can create expression trees in several ways:

  1. Programmatically: Using the classes in the System.Linq.Expressions namespace.
  2. From Lambda Expressions: By assigning a lambda expression to a variable of type Expression.

Example 1: Programmatic Creation

using System;
using System.Linq.Expressions;

public class ExpressionTreeExample
{
    public static void Main(string[] args)
    {
        // Represents x + y
        ParameterExpression paramX = Expression.Parameter(typeof(int), "x");
        ParameterExpression paramY = Expression.Parameter(typeof(int), "y");
        BinaryExpression body = Expression.Add(paramX, paramY);

        // Creates the lambda expression: (x, y) => x + y
        Expression> addExpression =
            Expression.Lambda>(body, paramX, paramY);

        Console.WriteLine("Expression: " + addExpression.ToString());

        // Compile the expression tree into a delegate
        Func addFunc = addExpression.Compile();

        // Execute the compiled delegate
        int result = addFunc(5, 10);
        Console.WriteLine("Result: " + result); // Output: Result: 15
    }
}

Example 2: From a Lambda Expression

This is often the most convenient way to create expression trees for LINQ queries.

using System;
using System.Linq.Expressions;

public class LambdaExpressionTreeExample
{
    public static void Main(string[] args)
    {
        // Assign a lambda expression to an Expression>
        Expression> addExpression = (x, y) => x + y;

        Console.WriteLine("Expression: " + addExpression.ToString());

        // Compile and execute
        Func addFunc = addExpression.Compile();
        int result = addFunc(7, 3);
        Console.WriteLine("Result: " + result); // Output: Result: 10
    }
}

Use Cases

Note: Expression trees are a cornerstone of modern C# development, particularly for data querying and manipulation. Understanding them unlocks advanced capabilities in various .NET frameworks.

Further Reading