Skip to main content
expression trees c#

Expression Trees in C# with an example

Before beginning with Expression Trees, let us first understand Expression in .NET

In .NET, Expression is an abstract class which contains static methods and inherited by various types (like ParameterExpression, MethodCallExpression, BinaryExpression etc.) to create expression tree nodes of specific types. Also, all these expression-specific expression tree types are defined in System.Linq.Expressions namespace.

Expression Trees was first introduced in C# 3.0 (Visual Studio 2008), where they were mainly used by LINQ providers. To know more about C# features with respect to its version refer the below post.

List of C# features version by version

What are Expression Trees in C#?

Expression trees represent code in a tree-like format, where each node is an expression (for example, a method call or a binary operation such as x < y).

You can also convert expression trees into compiled code and run it. This transformation enables dynamic modification of executable code as well as the execution of LINQ queries in various databases and the creation of dynamic queries.

Expression Trees in C# 3.0 #expressiontree #csharp Click To Tweet

Expression trees can be created by using following two ways:

  • Using Expression Lambda
  • Using Expression Tree API

1. Using Expression Lambda

The easiest way to generate an expression tree is to create an instance of the Expression<T> type, where T is a delegate type, and assign a lambda expression to this instance.

// Create an expression using expression lambda
Expression<Func<int, int, int>> expression = (num1, num2) => num1 + num2;

// Compile the expression
Func<int, int, int> compiledExpression = expression.Compile();

// Execute the expression.
int result = compiledExpression(3, 4); //return 7

In this example, the C# compiler generates the expression tree from the provided lambda expression. This looks much more complicated, but this is what actually happens when you supply a lambda expression to an expression tree.

The structure of above expression trees will be like as:

2. Using Expression Tree API

Expression class is used to create expression trees by using the API. In .NET Framework 4, the expression trees API also supports assignments and control flow expressions such as loops, conditional blocks, and try-catch blocks. By using the API, you can create expression trees that are more complex than those that can be created from lambda expressions. Using API, above code can be rewritten as:

//Create the expression parameters
ParameterExpression num1 = Expression.Parameter(typeof(int), "num1"); 
ParameterExpression num2 = Expression.Parameter(typeof(int), "num2");

//Create the expression parameters
ParameterExpression[] parameters = new ParameterExpression[] { num1, num2 };

//Create the expression body
BinaryExpression body = Expression.Add(num1, num2);</pre>

//Create the expression
Expression<Func<int, int, int>> expression =
Expression.Lambda<Func<int, int, int>>(body, parameters);

// Compile the expression
Func<int, int, int> compiledExpression = expression.Compile();

// Execute the expression
int result = compiledExpression(3, 4); //return 7

Expression Tree Structure

The simple structure of an Expression<TDelegate> has four properties as given below:

  1. Body: The body of the expression.
  2. Parameters: The parameters of the lambda expression.
  3. NodeType: The type of node in the tree
  4. Type: The type of the expression

Thanks for reading and let me know your valuable comments if any.

Like this post? Don’t forget to share it!

This is a guest post by Sanskriti. If you would like to write for DotnetCrunch, check our guest posting guidelines.

What do you think?

Dear Readers,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.

Happy Programming!

Was this article worth reading? Share it with fellow developers too.


I am a Software Engineer by profession. I love to write on .Net Technologies.