CSHARP -- Lambda expression

Source: Internet
Author: User
1. Definition:

A. Lambda expressions are anonymous expressions.

B. It can contain expressions and statements.

C. can be used to create a delegate or expression directory tree type

D. Syntax: Input Params => expression or code block. (Data parameter on the left and expression or statement block on the right .)

Eg:

// Multiple parameters (x, y) => X = y // No Params (int x, string S) => S. length> X // No parameter. Note: only one parameter can omit brackets: x => X * X () => somemethod () // statement block. Note: braces are used in the statement block. (Input parameters) =>{ statement ;}
Note: Lambda statements, like anonymous methods, cannot be used to create expression directory trees.
2. Simple use

A. Use in delegation (lambda expression)

       delegate int del(int i);       del myDelegate = x => x * x;       int j = myDelegate(5); //j = 25

B. delegation not used (lambda expression)

Delegate int del (int I); del mydelegate = delegate (int I) {I * I}; // anonymous method Int J = mydelegate (5); // J = 25
C. Create expression directory tree type:
         using System.Linq.Expressions;         // ...         Expression<del> = x => x * x;

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.