C#3.0 's magical lambda expressions and lambda statements

Source: Internet
Author: User

A LAMBDA expression is an anonymous function that can contain expressions and statements, and can be used to create a delegate or an expression tree type. All lambda expressions use the lambda operator =>, and the operator reads "goes to". The left side of the LAMBDA operator is the input parameter (if any), and the right side contains an expression or a block of statements. The LAMBDA expression x + x * x is read as "x goes to X times X". You can assign this expression to a delegate type.

The lambda expression on the right of an expression is called a lambda expression. Lambda expressions are widely used when constructing an expression tree. A LAMBDA expression returns the result of an expression and takes the following basic form:

(input parameters) = Expression

A lambda statement is similar to a lambda expression, except that the statement is enclosed in curly braces:

(input parameters) = {statement;}

the-= = operator hasthe same precedence as the assignment operator (=) and is the right associative operator.

Lambda is used in method-based LINQ queries as a where and where (IQueryable, String, array<object>[]()[]) Parameters such as standard query operator methods.

Lambda is not allowed on the left side of the IS or as operator.

All restrictions that apply to anonymous methods also apply to LAMBDA expressions.

delegate void Del (int x, int y);

Del d = (m, n) = = {int r = m + N; Response.Write (R); };
D (6, 4);
Result:10

int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0};
int oddnumbers = numbers. Count (n = = n% 2 = = 1);
Result:5

var lessThan6 = numbers. TakeWhile (n = n < 6);
Response.Write (Lessthan6.count ());
Result:4

delegate int del (int i);
Del mydelegate = x + x * x;
Response.Write (MyDelegate (5);
Result:25 Original: http://blog.itpub.net/12639172/viewspace-464474/

C#3.0 's magical lambda expressions and lambda statements

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.