[In-depth learning C #] expression tree type--expression tree types

Source: Internet
Author: User

An expression tree allows you to represent a lambda expression as a data structure rather than as executable code. The expression tree issystem.linq.expressions.expression< D >The value of the form's expression tree type (expression tree type), where D is any delegate type.
If there is a conversion from a lambda expression to a delegate type D, there is also a conversion to the expression tree type expression< D >. The conversion of a lambda expression to a delegate type produces a delegate that references the executable code of the lambda expression.A transform to the expression tree type creates an expression tree representation of the lambda expression.
   An expression tree is an efficient data representation of a lambda expression in memory, making the structure of a lambda expression transparent and clear.
  
As with delegate type D, expression< d > has the same parameters and return type as D.
The following example represents a lambda expression as executable code and an expression tree. Because there is a conversion to func< Int,int >, there is also a conversion to expression< func< int,int > >:
   func< int,int > del = x = x + 1;//Code
   expression< func< int,int > > exp = x = + x + 1;//Data
   after making the above assignment, the Delegate del Reference returns the method of X + 1, and the expression tree Exp reference describes the data structure of the expression x = x +1.
The precise definition of a generic type expression< D > and the exact rules for constructing an expression tree when you convert a lambda expression to an expression tree type are beyond the scope of this article and will be described otherwise.
There are two points that need to be clearly stated:
Not all lambda expressions can be converted to expression trees. For example, a lambda expression with a statement body and a lambda expression containing an assignment expression cannot be represented this way. In these cases, the conversion still exists, but will fail at compile time
expression< d > provides an instance method Compile, which produces a delegate of type D:
func< int,int > Del2 = Exp.compile ();
Calling this delegate causes the code represented by the expression tree to be executed. So, according to the definition above, Del and Del2 are equivalent, and
The following two statements will also be equivalent:
int i1 = del (1);
int i2 = DEL2 (1);
After executing this code, the values for both I1 and I2 are 2.

[In-depth learning C #] expression tree type--expression tree types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.