What is the difference between lambdas and delegates in the .NET Framework?

后端 未结 17 1361
小蘑菇
小蘑菇 2020-12-12 10:45

I get asked this question a lot and I thought I\'d solicit some input on how to best describe the difference.

17条回答
  •  一向
    一向 (楼主)
    2020-12-12 11:15

    I assume that your question concerns c# and not .NET, because of the ambiguity of your question, as .NET does not get alone - that is, without c# - comprehension of delegates and lambda expressions.

    A (normal, in opposition to so called generic delegates, cf later) delegate should be seen as a kind of c++ typedef of a function pointer type, for instance in c++ :

    R (*thefunctionpointer) ( T ) ;
    

    typedef's the type thefunctionpointer which is the type of pointers to a function taking an object of type T and returning an object of type R. You would use it like this :

    thefunctionpointer = &thefunction ;
    R r = (*thefunctionpointer) ( t ) ; // where t is of type T
    

    where thefunction would be a function taking a T and returning an R.

    In c# you would go for

    delegate R thedelegate( T t ) ; // and yes, here the identifier t is needed
    

    and you would use it like this :

    thedelegate thedel = thefunction ;
    R r = thedel ( t ) ; // where t is of type T
    

    where thefunction would be a function taking a T and returning an R. This is for delegates, so called normal delegates.

    Now, you also have generic delegates in c#, which are delegates that are generic, i.e. that are "templated" so to speak, using thereby a c++ expression. They are defined like this :

    public delegate TResult Func(T arg);
    

    And you can used them like this :

    Func thefunctor = thefunction2; // call it a functor because it is
                                                    // really as a functor that you should
                                                    // "see" it
    double y = thefunctor(2.0);
    

    where thefunction2 is a function taking as argument and returning a double.

    Now imagine that instead of thefunction2 I would like to use a "function" that is nowhere defined for now, by a statement, and that I will never use later. Then c# allows us to use the expression of this function. By expression I mean the "mathematical" (or functional, to stick to programs) expression of it, for instance : to a double x I will associate the double x*x. In maths you write this using the "\mapsto" latex symbol. In c# the functional notation has been borrowed : =>. For instance :

    Func thefunctor = ( (double x) => x * x ); // outer brackets are not
                                                               // mandatory
    

    (double x) => x * x is an expression. It is not a type, whereas delegates (generic or not) are.

    Morality ? At end, what is a delegate (resp. generic delegate), if not a function pointer type (resp. wrapped+smart+generic function pointer type), huh ? Something else ! See this and that.

提交回复
热议问题