I get asked this question a lot and I thought I'd solicit some input on how to best describe the difference.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
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:
is a lot more concise than what you can do with a delegate:
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
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#.
One difference is that an anonymous delegate can omit parameters while a lambda must match the exact signature. Given:
you can call it in the following four ways (note that the second line has an anonymous delegate that does not have any parameters):
You cannot pass in a lambda expression that has no parameters or a method that has no parameters. These are not allowed:
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++ :typedef's the type
thefunctionpointer
which is the type of pointers to a function taking an object of typeT
and returning an object of typeR
. You would use it like this :where
thefunction
would be a function taking aT
and returning anR
.In c# you would go for
and you would use it like this :
where
thefunction
would be a function taking aT
and returning anR
. 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 :
And you can used them like this :
where
thefunction2
is a function taking as argument and returning adouble
.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 adouble x
I will associate thedouble
x*x
. In maths you write this using the "\mapsto" latex symbol. In c# the functional notation has been borrowed :=>
. For instance :(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.
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,
The first line produces a delegate, while the second produces an expression tree.