What Are Expression Trees
Understand code represented as a data structure.
Code as Data
An expression tree represents code as a data structure you can inspect at runtime, instead of compiled instructions you can only run. C# models this with Expression<TDelegate>.
Lambda to a Delegate vs a Tree
Assign a lambda to a Func<...> and you get runnable code. Assign the same lambda to Expression<Func<...>> and the compiler builds a tree describing it.
using System;
using System.Linq.Expressions;
Func<int, int> runnable = x => x + 1; // compiled delegate
Expression<Func<int, int>> tree = x => x + 1; // data structure
Console.WriteLine(runnable(5)); // 6
Console.WriteLine(tree); // x => (x + 1)All lessons in this course
- What Are Expression Trees
- Building Expressions Manually
- Compiling and Executing Expressions
- Expression Trees in LINQ Providers