0Pricing
C# Academy · Lesson

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

  1. What Are Expression Trees
  2. Building Expressions Manually
  3. Compiling and Executing Expressions
  4. Expression Trees in LINQ Providers
← Back to C# Academy