Building Expressions Manually
Construct expression trees with the Expression API.
Building Expressions Manually is a free C# Academy lesson on CoddyKit — lesson 2 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.
Building Trees by Hand
Besides letting the compiler build a tree from a lambda, you can construct one yourself using the static factory methods on Expression.
using System;
using System.Linq.Expressions;
var five = Expression.Constant(5);
Console.WriteLine(five); // 5
Console.WriteLine(five.Type); // System.Int32Expression.Parameter
Expression.Parameter(type, name) creates a parameter node you can reference in the body and bind in a lambda.
using System;
using System.Linq.Expressions;
var x = Expression.Parameter(typeof(int), "x");
Console.WriteLine(x.Name + " : " + x.Type); // x : System.Int32Expression.Constant
Expression.Constant(value) embeds a literal. You can optionally specify the type.
using System;
using System.Linq.Expressions;
var c = Expression.Constant(42);
var typed = Expression.Constant(3.14, typeof(double));
Console.WriteLine(c + " " + typed); // 42 3.14Expression.Add
Binary operators have factories like Expression.Add. They take two operand expressions and build a BinaryExpression.
using System;
using System.Linq.Expressions;
var x = Expression.Parameter(typeof(int), "x");
var sum = Expression.Add(x, Expression.Constant(1));
Console.WriteLine(sum); // (x + 1)Other Binary Operators
There are factories for the full set: Subtract, Multiply, Divide, GreaterThan, Equal, and more.
using System;
using System.Linq.Expressions;
var x = Expression.Parameter(typeof(int), "x");
var gt = Expression.GreaterThan(x, Expression.Constant(10));
var mul = Expression.Multiply(x, Expression.Constant(2));
Console.WriteLine(gt + " | " + mul); // (x > 10) | (x * 2)Wrapping in a Lambda
Expression.Lambda ties a body and parameters into an Expression<TDelegate> you can later compile.
using System;
using System.Linq.Expressions;
var x = Expression.Parameter(typeof(int), "x");
var body = Expression.Add(x, Expression.Constant(1));
var lambda = Expression.Lambda<Func<int, int>>(body, x);
Console.WriteLine(lambda); // x => (x + 1)Composing Nested Expressions
Factories nest freely, so you can build complex expressions like (x + 1) * 2 step by step.
using System;
using System.Linq.Expressions;
var x = Expression.Parameter(typeof(int), "x");
var addOne = Expression.Add(x, Expression.Constant(1));
var timesTwo = Expression.Multiply(addOne, Expression.Constant(2));
Console.WriteLine(timesTwo); // ((x + 1) * 2)Member Access
Expression.Property and Expression.Field build member-access nodes.
using System;
using System.Linq.Expressions;
var s = Expression.Parameter(typeof(string), "s");
var lengthAccess = Expression.Property(s, "Length");
Console.WriteLine(lengthAccess); // s.LengthMethod Calls
Expression.Call builds an invocation node, given the target, method, and arguments.
using System;
using System.Linq.Expressions;
var s = Expression.Parameter(typeof(string), "s");
var toUpper = typeof(string).GetMethod("ToUpper", Type.EmptyTypes);
var call = Expression.Call(s, toUpper);
Console.WriteLine(call); // s.ToUpper()Type Checking at Build Time
The factories validate operand types as you build. Mismatched types throw immediately, catching errors early.
using System;
using System.Linq.Expressions;
try {
var bad = Expression.Add(
Expression.Constant(1),
Expression.Constant("text")); // int + string
} catch (InvalidOperationException) {
Console.WriteLine("type mismatch rejected");
}Putting It Together
Build the lambda x => (x + 1) * 2 entirely by hand.
using System;
using System.Linq.Expressions;
var x = Expression.Parameter(typeof(int), "x");
var body = Expression.Multiply(
Expression.Add(x, Expression.Constant(1)),
Expression.Constant(2));
var lambda = Expression.Lambda<Func<int, int>>(body, x);
Console.WriteLine(lambda); // x => ((x + 1) * 2)Quick Check
Confirm how manual expression building works.
Recap
You learned to build expression trees manually.
Expression.Parameter,Expression.Constantcreate leaf nodes.Expression.Addand friends build binary operations.Expression.Property,Expression.Callhandle members and methods.Expression.Lambdaassembles a callable tree.
Frequently asked questions
Is the “Building Expressions Manually” lesson free?
Yes — the full text of “Building Expressions Manually” 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 “Building Expressions Manually”?
Construct expression trees with the Expression API. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building Expressions Manually” 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