Delegates and lambda expressions

Source: Internet
Author: User

~, not to mention the delegate and lambda expression, first look at two examples:1. With a delegate, add 10 to a number and the following code:
classProgram {Private Delegate int Jisuan(inta); Static voidMain (string[] args) {Jisuan js=Zengshi; Console.WriteLine (JS (Ten));        Console.readkey (); }
Public Static intZengshi (inta) {returnA +Ten; } }
2. Using a lambda expression, add 10 to a number with the following code:

  

 class   program { private  delegate  int  Jisuan ( Span style= "color: #0000ff;"        >int   a);  static  void  Main (string  [] args) {Jisuan js  = P=>p+10  ;            Console.WriteLine (JS ( 10  

The P in Example 2 (p=>p+10) above is the argument in the example 1 delegate method, and (P+10) is the return value in the example 1 delegate method, where (= =) is the legendary lambda operator. With this explanation, we don't know if you know the connection between a delegate and a lambda expression. Yes, a lambda expression is another way of expressing a delegate, mainly to make the code simple and elegant. Of course, since p is a parameter, then in the case of multiple parameters, to pass multiple arguments, the parameters are written in parentheses, and then separated by commas, such as: (P,Q) =>p+q+10;

Add one point:

The left side of the lambda operator is the passed-in argument, and the right side is the body of the lambda expression, which can be an expression on the right, or a piece of code with a return value. I don't have an example here.

Delegates and lambda expressions

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.