Different encoding styles-Lambda expressions, encoding-lambda expressions

Source: Internet
Author: User

Different encoding styles-Lambda expressions, encoding-lambda expressions

Lambda expressions are also one of the most important features in C #3.0.

1. Introduction to Lambda expressions

Lambda expressions can be understood as an anonymous method. They can contain expressions and statements and are used to create a delegate or convert to an Expression Tree. When Lambda expressions are used, the "=>" operator is used. The left side of the operator is the input parameter of the anonymous method, and the right side is the expression or statement block.

1.1 Evolution of Lambda expressions

Lambda expressions are actually the reincarnation of anonymous methods. The following code is used for identification:

1 C #1.0 2 Func <string, int> delegatetest1 = new Func <string, int> (Callbackmethod); 3 4 C #2.0 5 Func <string, int> delegatetest2 = delegate (string text) 6 {7 return text. length; 8} 9 10 C #3.011 Func <string, int> delegatetest3 = (string text) => text. length; 12 13 can omit the parameter type string to simplify the code again 14 Func <string, int> delegatetest4 = (text) = text. length; 15 at this time, you can also omit 16 Func <string, int> delegatetest = text => text. length;

The code above vividly concealed how Lambda expressions evolved from anonymous methods.

 

1.2 use of Lambda expressions

In the actual development process, the purpose of delegation is to subscribe to events. To deepen your understanding of Lambda expressions, we will use Lambda expressions to subscribe to events.

1 namespace Lambda expression 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 Button button1 = new Button () {Text = "Click me "}; 8 button1.Click + = (sender, e) => ReportEvent ("Click Event", sender, e); 9 button1.KeyPress + = (sender, e) => ReportEvent ("KeyPress event", sender, e); 10} 11 12 private static void ReportEvent (string s, object sender, EventArgs eventArgs) 13 {14 throw new NotImplementedException (); 15} 16} 17}

From the code above, we can see that after using the C #3.0 object initializer and Lambda expressions, the code is indeed much more concise.

2. Expressions also have a tree structure-Expression Tree

Lambda expressions can be used not only to create delegates, but also to convert them into expression trees. The Expression Tree (or "expression directory tree") is a data structure used to represent the Lambda expression logic. It expresses the Code as an object tree rather than executable code. In fact, the expression tree can be understood as a data structure, similar to stack or queue.

2.1 construct an expression tree using Lambda expressions

1 namespace Expression Tree 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // construct a Lambda Expression into an Expression Tree 8 Expression <Func <int, int, int> expressionTree = (a, B) => a + B; 9 10 // obtain the Expression Tree parameter 11 Console. writeLine ($ "parameter 1: {expressionTree. parameters [0]}, parameter 2: {expressionTree. parameters [1]} "); 12 13 // obtain the subject part of the Expression Tree 14 BinaryExpression body = (BinaryExpression) expressionTree. body; 15 16 // left node (each node itself is an expression object) 17 ParameterExpression left = (ParameterExpression) body. left; 18 19 // right node 20 ParameterExpression right = (ParameterExpression) body. right; 21 22 Console. writeLine ("Expression Tree subject:"); 23 Console. writeLine (expressionTree. body); 24 Console. writeLine ($ "Expression Tree left node: {left. name} {Environment. newLine} node type: {left. type} {Environment. newLine} {Environment. newLine} Expression Tree right node: {right. name} {Environment. newLine} node type: {right. type} {Environment. newLine} "); 25 Console. readKey (); 26 27} 28} 29}

From the code above, we can see that the process of constructing the expression tree through Lambda expressions is very simple. You only need to assign the Lambda expression to an Expression Tree variable.

 

2.2 how to convert the expression tree into executable code

1 namespace Expression Tree 2 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // construct the Lambda Expression into the Expression Tree 8 Expression <Func <int, int, int> expressionTree = (a, B) => a + B; 9 // call the complie method to generate the Lambda expression delegate 10 Func <int, int, int> delinstance = expressionTree. compile (); 11 // call the delegate instance to obtain the result 12 int result = delinstance (1, 2); 13 Console. writeLine ($ "The sum of 2 and 3: {result}"); 14 Console. readKey (); 15} 16} 17}

The above Code uses the Compile () method of the Expression <TDelegate> class to Compile the Expression tree into a delegate instance, and then obtains the sum of two numbers through the delegate call method.

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.