Take the 2-minute tour ×

By just briefing a book (leading one) , one thing caught my eyes - the lambda expression definition :

A lambda expression is an unnamed method written in place of a delegate instance.

in place of delegate instance ???

A delegate instance is an object that refers-to/encapsulate target method/s :

In the following sample The right side(where the lambda expression would be) is not a delegate instance. it is a method.

TransformerDelegate t = SquareMethod;

So the definition should have been corrected-to/mention :

lambda expression are unnamed method written in place of a method(!) being referenced by delegate variable.

TransformerDelegate sqr = x => x * x;

            this is the location for method/anonymous methods.

do you see what I mean ? Am I right?

p.s. I did understand the msdn's one : ( but want to see if the book had made a mistake)


share|improve this question
You have a sharp eye for this sort of subtle issue; you are correct that the phrasing is inelegant and inexact, even if it gets the right idea across. If you read technical books closely you will very quickly find lots of these sorts of issues; even experienced writers confuse "object" with "variable", for instance. The only book I have edited that used technical terms correctly pretty much every time was of course C# in Depth. :-) –  Eric Lippert Dec 20 '12 at 15:05
@EricLippert Thanks Eric :-) –  Royi Namir Dec 20 '12 at 15:21
Pedantry: in the statement TransformerDelegate t = SquareMethod;, SquareMethod is not a method, it is a method group. –  phoog Dec 20 '12 at 19:26
@phoog why do you say it is a method group ? the TransformerDelegate type has a strict definition . for example : public delegate int TransformerDelegate (int a) it will only execute for ints. Did you mean that it can be execute with less derived types ? but what is less derived than int ? –  Royi Namir Dec 21 '12 at 8:13
Eric Lippert will probably correct me here but I think @phoog means that the name SquareMethod can be several overloaded methods and is therefore called a MethodGroup even if it just one method in this case. (Only one of the overloaded methods can match the delegate signature so only one is added to the delegate) –  adrianm Dec 21 '12 at 13:13
show 1 more comment

3 Answers

up vote 4 down vote accepted

The value of a lambda expression is a delegate instance.

So the book is probably referring to code like:

MySquareDelegate f1 = x => x * x;
MySquareDelegate f2 = new MySquareDelegate(MySquareMethod);
MySquareDelegate f3 = MySquareMethod;  // just shorthand for the previous line

It is tricky stuff to explain in 1 sentence, your own version


is talking about "a method instead of a method", the original about "a method instead of a delegate instance" where the method is implicitly converted to a delegate instance. Both seem incomplete at least.

A definition of a lambda should also include that it is an inline method.

share|improve this answer
+1 makes sense. in my head it was MySquareDelegate f2 = MySquareMethod and in the right side is just a method. but the old fashioned way( using new() ) clarified things. thanks . so the book is not wrong. –  Royi Namir Dec 20 '12 at 13:02
add comment

Other answers miss the fact that lambda expressions do not necessarily represent methods. Sometimes, they represent expression trees.

The compiler implicitly converts lambda expressions to one type of object or the other, depending on context.

To specify a method, you need to specify parameters and a body. In a lambda expression, these are separated by =>. Examples:

    ()     => 4;               //empty parameter list
    x      => x.ToString();    //one parameter
    (a, b) => a.Equals(b);     //two parameters
//  |                    |
//  parameter list       body

These lambda expressions can be converted to Func<int>, Func<object, string>, and Func<object, object, bool> respectively. The could also be converted to Expression<Func<int>>, Expression<Func<object, string>>, and Expression<Func<object, object, bool>> respectively.

An anonymous method:

delegate (object p, object q) { return string.Concat(p, q); }
//       parameter list       body

Here are two examples of lambda conversion:

Func<object, object, bool> aDelegate = (o1, o2) => object.Equals(o1, o2);
Expression<Func<object, object, bool>> anExpressionTree = (o1, o2) => object.Equals(o1, o2);

In method group conversion, the parameters and method body are specified by overload resolution. To simplify a bit, the compiler looks at the methods with the indicated name and chooses the correct overload based on the context. For example, consider these overloads of SquareMethod:

int SquareMethod(int a) { return a * a; }
double SquareMethod(double a) { return a * a; } 

These statements involve method group conversion and overload resolution:

Func<int, int> squareAnInt = SquareMethod;
Func<double, double> squareADouble = SquareMethod;

Finally, statement lambdas cannot be translated to expression trees:

Action<object> anAction = o => { Console.WriteLine(o); };
Func<object, int> aFunc = o =>
        var s = (o ?? "").ToString();
        return s.Length;

The C# language specification (somewhat confusingly) uses the term "anonymous function" to cover both lambda expressions and anonymous methods. Anonymous functions can be implicitly converted to a compatible delegate type, and so can method groups. Therefore, if we have a delegate type called DelegateType, and a declaration/assignment like this:

DelegateType d = [something];

Then [something] can be a method group or an anonymous function. In other words, it can be a method group, an anonymous method or a lambda expression.

So, your correction to the book's text would be better to say "in place of a method group", but I would say

A lambda expression is an unnamed method that, like a named method group, can be used to create a delegate instance.

I might also add

In some cases, a lambda expression can be used to create an expression tree rather than a delegate instance.

share|improve this answer
add comment

Basically a delegate is nothing but an object that knows how to call a specific method. So a delegate instance literally acts as a delegate for the caller: the caller invokes the delegate, and then the delegate calls the target method.

share|improve this answer
"The object is built by the compiler" kind of glosses over the finer points, doesn't it? The object is built at run time, not compile time. –  phoog Dec 21 '12 at 21:52
add comment

Your Answer


