Expression trees in c#

Quick-start Expression Trees in C# 3.0 With an Example

Before beginning with Expression Trees in C#, 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,MethodCallExpressionBinaryExpression etc.) to create expression tree nodes of specific types.

Also, all these expression-specific expression tree types are defined in System.Linq.Expressions namespace.

Introduction

Expression Trees were 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 to 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.

How to create Expression Trees in C#?

Expression trees can be created by using the 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 the above expression trees will be like as shown below:


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, the 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); 
//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
0*58UfluqOeiehO1yN

That’s all on Expression Trees in C# from this post’s point of view.

Want to know more about expression trees in c# please refer to Microsoft Docs. Thanks for reading and let us know your valuable comments about this post.

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

Share with your friends:

3 thoughts on “Quick-start Expression Trees in C# 3.0 With an Example”

  1. Pingback: C# Version History: Detailed List of C# Features From Version 1 to 9 - DotNetCrunch

  2. Pingback: Classes and Constructors in C# Made Easy For You in 2020 - DotNetCrunch

  3. Pingback: Quick-start Extension Methods in C# 3.0: Here is what you can accomplish with them - DotNetCrunch

Leave a Comment

Your email address will not be published. Required fields are marked *