C # Programming Note (anonymous method, lambda expression)

Source: Internet
Author: User

1. Anonymous Methods is essentially a method where any delegate can be assigned using an anonymous method
1 Example: 2 // Anonymous methods also use the delegate keyword 3 func<int ,int ,intdelegate (int _x,int _y) 4 {5      return _x + _y; 6 };

2.Lambda-expression   can be used as an anonymous method instead Example:
1 //Anonymous Methods2func<int,int,int> =Delegate(int_x,int_y)3 {4      return_x +_y;5 };6 //lambda expression, no keyword7func<int,int,int> = (_x, _y)8 {9      return_x +_y;Ten};

  PS: As long as the delegate can be anonymous, as long as it is anonymous can be used lambda expression PS: When the function parameter has only one time, can not add parentheses:func<int,int> a = B=>{return B}; when the statement of a function body has only one sentence, it can not be enlarged by parentheses, or it may not be called return:func<int,int> a = b=>b;

C # Programming Note (anonymous method, lambda expression)

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.