What Are Expression Trees
Understand code represented as a data structure.
What Are Expression Trees is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)Inspecting the Tree
Because it is data, you can read the parts of an expression tree: its parameters, body, and node type.
using System;
using System.Linq.Expressions;
Expression<Func<int, int>> tree = x => x + 1;
Console.WriteLine(tree.Parameters[0].Name); // x
Console.WriteLine(tree.Body.NodeType); // Add
Console.WriteLine(tree.ReturnType); // System.Int32The Expression Hierarchy
Every node derives from System.Linq.Expressions.Expression. Common kinds include BinaryExpression, ParameterExpression, ConstantExpression, and MethodCallExpression.
using System;
using System.Linq.Expressions;
Expression<Func<int, int>> tree = x => x + 1;
var body = (BinaryExpression)tree.Body;
Console.WriteLine(body.Left); // x
Console.WriteLine(body.Right); // 1Only Expression Lambdas Allowed
You can only turn a single-expression lambda into a tree. Statement-bodied lambdas (with braces) cannot be expression trees.
using System;
using System.Linq.Expressions;
Expression<Func<int, int>> ok = x => x * 2; // fine
// Expression<Func<int,int>> bad = x => { return x*2; }; // compile error
Console.WriteLine(ok); // x => (x * 2)Why It Matters
Expression trees let libraries understand your code rather than just run it. LINQ providers translate them into SQL, dynamic queries, or other targets.
NodeType Tells You the Operation
The NodeType property is an ExpressionType enum value identifying the operation at that node.
using System;
using System.Linq.Expressions;
Expression<Func<int, bool>> tree = x => x > 10;
Console.WriteLine(tree.Body.NodeType); // GreaterThanConstants and Parameters
A ParameterExpression stands for an input; a ConstantExpression holds a literal value embedded in the code.
using System;
using System.Linq.Expressions;
Expression<Func<int, int>> tree = x => x + 5;
var body = (BinaryExpression)tree.Body;
Console.WriteLine(body.Left.GetType().Name); // ParameterExpression (or TypedParameterExpression)
Console.WriteLine(((ConstantExpression)body.Right).Value); // 5Method Calls in Trees
A lambda that calls a method becomes a MethodCallExpression, exposing the method and its arguments.
using System;
using System.Linq.Expressions;
Expression<Func<string, int>> tree = s => s.Length;
// member access here; a call example:
Expression<Func<string, string>> call = s => s.ToUpper();
var mc = (MethodCallExpression)call.Body;
Console.WriteLine(mc.Method.Name); // ToUpperTrees Are Immutable
Expression trees are immutable. To "change" one you build a new tree from its parts, which is how visitors and rewriters work.
Putting It Together
Here we examine a small predicate tree end to end: parameter, operator, and constant.
using System;
using System.Linq.Expressions;
Expression<Func<int, bool>> isAdult = age => age >= 18;
var body = (BinaryExpression)isAdult.Body;
Console.WriteLine(isAdult.Parameters[0].Name); // age
Console.WriteLine(body.NodeType); // GreaterThanOrEqual
Console.WriteLine(((ConstantExpression)body.Right).Value); // 18Quick Check
Confirm what makes an expression tree special.
Recap
You learned what expression trees are.
Expression<TDelegate>represents code as inspectable data.- Nodes include parameters, constants, binary ops, and method calls.
- Only single-expression lambdas qualify.
- Trees are immutable; rewriting builds new trees.
Frequently asked questions
Is the “What Are Expression Trees” lesson free?
Yes — the full text of “What Are Expression Trees” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “What Are Expression Trees”?
Understand code represented as a data structure. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “What Are Expression Trees” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Are Expression Trees
- Building Expressions Manually
- Compiling and Executing Expressions
- Expression Trees in LINQ Providers