Object-Oriented Design-the original "Lambda expression" came in like this

Source: Internet
Author: User
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.
-- Msdn

The above is from msdn, we can see that the development of the Declaration delegate: naming method → anonymous method → Lambda expression. The code below demonstrates how they evolved.



Naming Method


For higher-level face objects, delegation is required. Then, let's take the implementation of "finding the maximum number of two numbers" as an example to see the original delegate implementation method.

Declare a member method:

public int CompareTwoValue(int a, int b){    if (a > b)    {        return a;    }    else    {        return b;    }}

Declare another delegate:

public delegate int deleMethod(int a,int b);

Call the preceding member methods by Delegate.Note that the parameter type and return type of the method must match the delegate parameter and return type. In this example, both the member method and delegate input two int values and output an int value.

Public int show () {delemethod max = new delemethod (comparetwovalue); // instantiate return max (5, 10); // call}


Anonymous Delegation


This is also called the anonymous method. In fact, this is only a change in the syntax form. Because type matching is still required during compilation, for the show method above, I can use anonymous delegation to express it.

<pre name="code" class="csharp">public int Show(){    deleMethod max = delegate(int a, int b)    {        if (a > b)        {            return a;        }        else        {            return b;        }    };    return max(5, 10);}
 
 

Obviously, we can't see the naming conventions. The syntax is simplified by using the anonymous method. However, we don't think that anonymous delegation is of substantial use in C #2.0. Only by 3.0, lamda expressions are actually useful.

Lambda expressions

Use the Lamda expression to write the following method:

public int Show(){    Func<int, int, int> max = (a, b) =>    {        if (a > b)        {            return a;        }        else        {            return b;        }    };    return max(5, 10);}

Lamda expressions are only a manifestation of anonymous delegation. We can intuitively see that the Lamda expression has the least amount of code in the three methods.


Since we have already indicated that the max type is func <int, Int, int>, the C # compiler can clearly know that both A and B are of the int type, so we can save the type information before the parameter. This feature is called" Type deduction", That is, the compiler can automatically know the types of some members. Please do not think that this small improvement is of little significance. In fact, you will find that the advantages of lambda expressions are made up of 1.1 pieces of details. Then let's change again:
Func<int, int, int> max = (a, b) => a > b ? a : b;
If the body of a Lambda expression is an expression rather than a statement, its body can omit braces and return keywords. In addition, if the lambda expression contains only one parameter, the brackets in the parameter list can also be omitted, as shown below:
Func<int, bool> positive = a => a > 0;
Is it very simple to write today? Let's take a look at what it will look like if you use the delegate Keyword:
Func<int, bool> positive = delegate(int a){    return a > 0;};
You can immediately realize that, the difference between this line and the multi-line, the omission of these keywords and parentheses will make the programming world quite different at once. (Of course, lambda expressions do not completely replace the delegate method. For example, if an anonymous method with the ref and out keywords is used, you must use the delegate in. NET 2.0 to construct it .)


Summary

One sentence: it can improve readability and reduce the number of code. I can't find any reason to reject lambda expressions.

Object-Oriented Design-the original "Lambda expression" came in like this

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.