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
,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.
Topics Covered
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.
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:
- Body: The body of the expression.
- Parameters: The parameters of the lambda expression.
- NodeType: The type of node in the tree
- Type: The type of the expression
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!
Pingback: C# Version History: Detailed List of C# Features From Version 1 to 9 - DotNetCrunch
Pingback: Classes and Constructors in C# Made Easy For You in 2020 - DotNetCrunch
Pingback: Quick-start Extension Methods in C# 3.0: Here is what you can accomplish with them - DotNetCrunch