A preliminary understanding of Lambda expressions in Linq, linqlambda expressions

Source: Internet
Author: User

A preliminary understanding of Lambda expressions in Linq, linqlambda expressions
Anonymous Method

The anonymous method, as its name implies, does not have a name, but still has a method body that can work. You may have seen it in many places, such as js!

Let's look at what Msdn says:

In C # versions earlier than 2.0, the only way to declare a delegate is to use the naming method. C #2.0 introduces anonymous methods. In C #3.0 and later versions, Lambda expressions replace anonymous methods as the preferred method for writing Inline code. However, the information about anonymous methods in this topic also applies to Lambda expressions. In one case, the anonymous method provides functions not available in Lambda expressions. You can use the anonymous method to ignore the parameter list. This means that the anonymous method can be converted to a delegate with various signatures. This is not possible for Lambda expressions.

Example
Namespace Wolfy. linqDemo {/// <summary> /// create a delegate Del /// </summary> /// <param name = "x"> parameter </param> public delegate void Del (int x ); class Program {static void Main (string [] args) {// use the anonymous method to create a delegate object d Del d = delegate (int x) {Console. writeLine (x );};}}}

By using the anonymous method, you do not need to create a separate method, which reduces the coding system overhead required for instantiating the delegate.

For example, a thread class can create a thread and contain the code executed by the thread.

Thread thread = new Thread (delegate () {// method body Console. WriteLine ("Hello world ");});

It can be more straightforward to understand. If this method is used once, you can use the anonymous method.

Lambda Definition

Lambda expressions are anonymous functions that can be used to create a delegate or expression directory tree.

To create a Lambda expression, specify the input parameter on the left side of the Lambda operator => (if any), and then enter the expression or statement block on the other side.

Example
/// <Summary> /// create a delegate Del /// </summary> /// <param name = "x"> parameter </param> public delegate void Del (int x ); class Program {static void Main (string [] args) {Del del = x => Console. writeLine (x * x); del (2); // 4 Console. read ();}}

To create an expression directory tree, you can:

/// <Summary> /// create a delegate Del /// </summary> /// <param name = "x"> parameter </param> public delegate void Del (int x ); class Program {static void Main (string [] args) {Del del = x => Console. writeLine (x * x); del (2); // 4 System. linq. expressions. expression <Del> expression = x => Console. writeLine (x); Console. read ();}}

In the preceding example, an expression is created for the directory tree object expression. Because the Del delegate does not return a value, it is output directly here.

  Expression Lambda

The lambda expression at the right of the => operator is called "expression lambda ". Lambda returns the result of the expression in the following basic form:

(input parameters) => expression

  Note: parentheses are optional only when lambda has only one input parameter; otherwise, parentheses are required. Two or more input parameters in parentheses are separated by commas:

 (x, y) => x == y
// Sometimes, the compiler is difficult or cannot infer the input type. In this case, you can explicitly specify the type (int x, string s) => s. length> x // use null parentheses to specify zero input parameters () => SomeMethod ()

  Statement Lambda

The statement lambda is similar to the expression lambda expression, but the statement is enclosed in braces.

 (input parameters) => {statement;}

Statement (statement Lambda) can contain any number of statements, but generally there are no more than two or three.

Example

Namespace Wolfy. linqDemo {/// <summary> /// create a delegate Del /// </summary> /// <param name = "x"> parameter </param> public delegate void Del (string strName ); class Program {static void Main (string [] args) {Del d = x => {string s = "Hello" + "" + x; Console. writeLine (s) ;}; d ("wolfy"); Console. read ();}}}

Note:

Like an anonymous method, statement lambda cannot be used to create an expression directory tree.

Summary

Lambda and anonymous methods are briefly introduced here. Although it is often used in projects, the basic knowledge of Lambda still needs to be supplemented.

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.