What is the difference between lambdas and delegat

2019-01-29 18:12发布

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

标签: c# .net lambda
17条回答
来,给爷笑一个
2楼-- · 2019-01-29 18:50

Lambdas are simplified versions of delegates. They have some of the the properties of a closure like anonymous delegates, but also allow you to use implied typing. A lambda like this:

something.Sort((x, y) => return x.CompareTo(y));

is a lot more concise than what you can do with a delegate:

something.Sort(sortMethod);
...

private int sortMethod(SomeType one, SomeType two)
{
    one.CompareTo(two)
}
查看更多
The star\"
3楼-- · 2019-01-29 18:51

It is pretty clear the question was meant to be "what's the difference between lambdas and anonymous delegates?" Out of all the answers here only one person got it right - the main difference is that lambdas can be used to create expression trees as well as delegates.

You can read more on MSDN: http://msdn.microsoft.com/en-us/library/bb397687.aspx

查看更多
干净又极端
4楼-- · 2019-01-29 18:52

Delegates are really just structural typing for functions. You could do the same thing with nominal typing and implementing an anonymous class that implements an interface or abstract class, but that ends up being a lot of code when only one function is needed.

Lambda comes from the idea of lambda calculus of Alonzo Church in the 1930s. It is an anonymous way of creating functions. They become especially useful for composing functions

So while some might say lambda is syntactic sugar for delegates, I would says delegates are a bridge for easing people into lambdas in c#.

查看更多
孤傲高冷的网名
5楼-- · 2019-01-29 18:53

One difference is that an anonymous delegate can omit parameters while a lambda must match the exact signature. Given:

public delegate string TestDelegate(int i);

public void Test(TestDelegate d)
{}

you can call it in the following four ways (note that the second line has an anonymous delegate that does not have any parameters):

Test(delegate(int i) { return String.Empty; });
Test(delegate { return String.Empty; });
Test(i => String.Empty);
Test(D);

private string D(int i)
{
    return String.Empty;
}

You cannot pass in a lambda expression that has no parameters or a method that has no parameters. These are not allowed:

Test(() => String.Empty); //Not allowed, lambda must match signature
Test(D2); //Not allowed, method must match signature

private string D2()
{
    return String.Empty;
}
查看更多
混吃等死
6楼-- · 2019-01-29 18:53

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<in T, out TResult>(T arg);

And you can used them like this :

Func<double, double> 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<double, double> 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.

查看更多
Root(大扎)
7楼-- · 2019-01-29 18:54

A delegate is always just basically a function pointer. A lambda can turn into a delegate, but it can also turn into a LINQ expression tree. For instance,

Func<int, int> f = x => x + 1;
Expression<Func<int, int>> exprTree = x => x + 1;

The first line produces a delegate, while the second produces an expression tree.

查看更多
登录 后发表回答