A preliminary understanding of the lambda Expression of LINQ

Source: Internet
Author: User

Anonymous methods

Anonymous methods as the name implies, there is no Name method, but still have the method body, still can work. In many places you may have seen it, such as JS, with the most!

Take a look at MSDN to say:

In the C # version prior to 2.0, the only way to declare a delegate was to use a named method. C # 2.0 introduces anonymous methods, whereas in C # 3.0 and later, LAMBDA expressions replace anonymous methods as the preferred way to write inline code. However, the information in this topic about anonymous methods also applies to LAMBDA expressions. In one case, an anonymous method provides functionality that is not available in a LAMBDA expression. You can use anonymous methods to ignore parameter lists. This means that anonymous methods can be converted to delegates with various signatures. This is not possible for Lambda expressions.

An example
namespace wolfy.linqdemo{//<summary>///    Create a Delegate del//</summary>//    <param name= "x" > Parameters </param> Public    delegate void Del (int x);    Class program    {        static void Main (string[] args)        {            //Use anonymous method to create a delegate object D            Del d = delegate (int x)            {                Console.WriteLine (x);            };}}    }

By using anonymous methods, you reduce the coding overhead required to instantiate a delegate because you do not have to create a separate method.

For example, a thread class can create a thread and contain the code that the thread executes.

             Thread thread = new Thread (                 delegate ()                 {                     //Method Body                     Console.WriteLine ("Hello World");                 });

It can be more straightforward to understand that if the method is used once, then the anonymous method can be used.

LAMBDA definition

A LAMBDA expression is an anonymous function that you can use to create a delegate or an expression tree type.

To create a lambda expression, you specify an input parameter, if any, on the left side of the lambda operator, and then enter the expression or statement block on the other side.

An example
<summary>///    Create a Delegate del///</summary>//    <param name= "x" > Parameters </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 tree, you can:

<summary>///    Create a Delegate del///</summary>//    <param name= "x" > Parameters </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 ();        }    }

The above example creates an expression directory tree object expression, because the Del delegate is not a return value, here is the direct output.

  An expression lambda

The lambda expression that the expression is on the right side of the = = operator is called expression Lambda. The expression lambda returns the result of an expression and takes the following basic form:

(input parameters) = Expression

  Note: Parentheses are optional only if the lambda has only one input parameter, otherwise parentheses are required. Two or more input parameters in parentheses are delimited by commas:

(x, y) = x = = Y
             Sometimes, it is difficult or impossible for the compiler to infer the input type. If this is the case, you can explicitly specify the type             (int x, string s) as shown in the following example = = s.length > x             //Use empty brackets to specify 0 input parameters             () = SomeMethod ()

  Statement Lambda

Statement Lambda is similar to an expression lambda expression except that the statement is enclosed in curly braces

(input parameters) = {statement;}

Statement (statement lambda) can contain any number of statements, but usually no more than two or three.

An example

namespace wolfy.linqdemo{//<summary>///    Create a Delegate del//</summary>//    <param name= "x" > Parameters </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 ();}}}    

Attention

Like anonymous methods, statement lambda cannot be used to create an expression tree

Summarize

Here's a brief introduction to lambda and anonymous methods. Although it is often used in projects, the basics of lambda still need to be mended.

A preliminary understanding of the lambda Expression of LINQ

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.