0Pricing
C# Academy · Lesson

Building Expressions Manually

Construct expression trees with the Expression API.

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.Int32

Expression.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.Int32

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