Five minutes to review the commission, anonymous methods, Lambda, generic delegates, expression trees

  These things for the older generation of programmers are commonplace, nothing new, but the new generation of programmers is full of charm. Once the new generation, many have been long learning, understanding, and practice to master the commission, these applications expression tree. Today, I try to describe what a simple way, so that everyone reading this blog in five minutes.

First minute: commission

  Some textbooks, blog commission will be referred to when it comes to events, although the event is an instance of the commission, but in order to understand easier, but only the commission today to talk about the event. The first piece of code:

The code below, a delegate to complete the demo application. A commission is divided into three steps:

public  partial  class WebForm3: System.Web.UI.Page 
{ 
    // Step01: First, define a delegate with delegate. 
    public  the delegate  int CalculatorAdd ( int X, int Y); 

    protected  void the Page_Load ( Object SENDER, EventArgs E) 
    { 
        // step03: use this method to instantiate a delegate. 
        CADD = CalculatorAdd new new CalculatorAdd (the Add);
         // int CADD Result = (. 5,. 6); 
        int Result = cAdd.Invoke ( . 5 , . 6 ); 
    } 
    // step02: a method corresponding to the delegate declaration. 
    public int Add(int x, int y)
    {
        return x + y;
    }
}

step01: First, a delegate by delegate definition.

step02: a way to declare a corresponding delegate.

step03: use this method to instantiate a delegate.

At this point, a delegate should complete, you can call the commission.

Second minute: Anonymous Methods

  In a minute already know, to complete a three-step application delegate, missing a step will not work, if you want to cross a big step, a big step beware pulled eggs. But Microsoft is not afraid of inviting egg, made of non-take three steps to two steps walked! So Microsoft will use anonymous three ways to simplify the procedure above. How do you say this stuff anonymous methods in C #, is completely non-essential things, just as C # icing on the cake, it was fantastic and give it a name is called syntax sugar.

public  partial  class WebForm3: System.Web.UI.Page 
{ 
    // Step01: First, define a delegate with delegate 
    public  delegate  int CalculatorAdd ( int X, int Y); 

    protected  void the Page_Load ( Object SENDER, EventArgs E) 
    { 
        // step02: {x return + y;} with the wording delegate (int x, int y) , to a method of assigning delegate 
        CalculatorAdd CADD = the delegate ( int X, int Y) { return X + Y;};
         int Result = CADD. the Invoke ( . 5 , . 6);
    }
}

step01: First, a delegate by delegate definition.

step02: with the wording delegate (int x, int y) {return x + y;}, the method of assigning a delegate, in fact, such an approach is anonymous methods.

At this time you will be surprised to find that this is not a three-step two-step in front of the wow?

Third minute: Lambda Expressions

  Had a very simple program, add a few delegate keyword, this code is about to become esoteric, esoteric things that people who understand fewer, so this can also be used as a bargaining chip raise. But Microsoft's design philosophy of C # is simple to use. Microsoft is trying to simplify the delegate (int x, int y) {return x + y;} anonymous methods, Lambda appeared. I see a few below lambda expression is written:

public partial class WebForm3 : System.Web.UI.Page
{
    public delegate int CalculatorAdd(int x, int y);

    protected void Page_Load(object sender, EventArgs e)
    {
        //方法一:
        CalculatorAdd cAdd1 = (int x, int y) => { return x + y; };
        int result1 = cAdd1(5, 6);

        //方法二:
        CalculatorAdd cAdd2 = (x, y) => { return x + y; };
        int result2 = cAdd2(5, 6);

        //方法三:
        CalculatorAdd cAdd3 = (x, y) => x + y;
        int result3 = cAdd2(5, 6);
    }
}

 

Method a: delegate removed simply put, between () and add {} "=>."

Method 2: a method based on the parameter type are blown away.

Method three: a pound to more thoroughly, the {}, and a return key are removed.

These methods just how to write all right, but that is the harm done to beginners, while seeing such an approach, while seeing the kind of writing, the people who engage in head over heels, and if no one pointing, indeed confused, difficult the rub.

Fourth minute: generic delegate

  With the .net version does not upgrade, the new version of the always different from the old version of it, or Microsoft's engineers how to cross their boss ah? So Microsoft has to play new tricks.

public partial class WebForm3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //方法一:
        Func<int,int,int> cAdd1 = (int x, int y) => { return x + y; };
        int result1 = cAdd1(5, 6);

        //方法二:
        Func<int, int, int> cAdd2 = (x, y) => { return x + y; };
        int result2 = cAdd2(5, 6);

        //方法三:
        Func<int, int, int> cAdd3 = (x, y) => x + y;
        int result3 = cAdd3(5, 6);
    }
}

  Whether or anonymous methods Lambda expressions, completed a delegate of application, can not escape two steps, step is to define a delegate, another step is to use a method to instantiate a delegate. Microsoft simply to have this two-step synthesis step to go. Func used to simplify the definition of a delegate.

  Thus an application can delegate with Func <int, int, int> cAdd3 = (x, y) => x + y; this statement to complete, which is called generic delegate Func.

       Action <> is a parameter and return value are generic void.

Fifth minute: expression tree

  In fact, the expression tree has no relationship with the client, have something to do, then so to speak, an expression tree is located delegate container. If I have to say is more professional, Lambda expressions Expression trees are accessing a data structure. Lambda expressions to use when, directly from the expression out, Compile () can be used directly. The following code:

public partial class WebForm3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Expression<Func<int, int, int>> exp = (x, y) => x + y;
        Func<int, int, int> fun = exp.Compile();
        int result = fun(2, 3);
    }
}

 

Transfer: https: //www.cnblogs.com/xcj26/p/3536082.html#

Guess you like

Origin www.cnblogs.com/sameLin/p/11771247.html