It is important to understand where lambda expressions come from and why they are used. It is true that most people hear about lambdas through LINQ, but LINQ just implements a feature that was introduced in version 3.0 of C # as a successor (I think) to anonymous methods.
Lambdas are actually used with delegate , and when you use lambda, what you actually do is assign a delegate method. However, you can avoid using the delegate keyword because the compiler can conclude that you are using delegate simply because you are using a lambda expression.
So the same thing:
MyDel del = delegate (int x) {return x + 1;} ;
Keep in mind that the delegate does not execute its methods until it is called. That way, even if you see x => x + 1 in your lambda expression, this actually does not work until you call le1(someInt) . You may have seen this in LINQ, where the request is not executed until the delegate is called.
In short (and I don't know, for my brevity), lambda allows you to express a method call in short form without having to declare it in the body of your class.
My source for this answer is if from Daniel Solis' Illustrated C # 2008 , pp. 375-377.
source share