2

If I would translate this anonymous method:

Func<int, int> f = delegate(int i)
{
     return i + 1;
};

into a lambda expression, it would like this:

Func<int, int> f = i => i + 1;

(I know: this lambda expression will secretly generate another anonymous method (by the compiler), but that is not the point).

Now I want to translate the following method into a lambda expression:

Func<int, int> f = delegate(int i)
{
     Debug.WriteLine("Inside the function!");
     return i + 1;
};

Is there a way and how do I do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
  • 2
    What do you mean by "a lambda expression will secretly generate another anymous method"? That's not really true... (There will be a method generated in the IL for *either* a lambda expression *or* an anonymous method, but it's not like lambda expressions are translated into anonymous methods which are then compiled as normal.) – Jon Skeet Apr 30 '13 at 10:02
  • @Jon: As far as I know a lambda expression is only created when the the type `f` is an `Expression`. In this case `f` is a delegate and this force the compile to create an anoymous method, is it not? – Martin Mulder Apr 30 '13 at 10:14
  • 1
    No, you're confused. `i => i + 1` *is* a lambda expression. Both lambda expressions and anonymous methods are *source level* constructs, and both can be converted into delegate types. Additionally, lambda expressions can be converted into expression trees. – Jon Skeet Apr 30 '13 at 10:15
  • @Jon: I agree to all what you are saying. But I still do not see what is "wrong" in my question. Would it change things if i replace the word "a" into "this" in that line? – Martin Mulder Apr 30 '13 at 10:19
  • The fact that you're talking about a lambda expression generating an anonymous method is either wrong, or just very badly phrased. It's not clear what you mean, and your earlier comment around "a lambda expression is only created..." is definitely wrong. – Jon Skeet Apr 30 '13 at 10:38
  • @Jon: Yes... perhapse my english is not that briljant. And with "lambda expression" I meant "expression tree". Further, please let us focus on what we mean, and not what we exactly say, because "both can be converted into delegate types" is also litteraly wrong. Nothing is converted into a (new) type. A piece of code is generated and the delegate is set with the reference to that code. – Martin Mulder Apr 30 '13 at 10:43
  • I didn't say anything about something being converted into a new type. I'm saying that the language specification provides a conversion from any anonymous function into a compatible delegate type. This isn't a matter of *English* - it's a matter of technical terms. Lambda expressions and expression trees are very different - did you actually mean expression tree *every* time you used lambda expression in your question, or only sometimes? Precision here is important. But are you actually satisfied with the (perfectly good) answer you got, or is there something else? – Jon Skeet Apr 30 '13 at 10:46
  • @Jon: You are right: it's a matter of technical terms and precision is important. But both things are expressed in language that is not my own, so I do not understand everyword I am reading and writing English will also result in errors sometimes. But I do my best. And you are right again: my poorly formed question resulted in a perfectly good answer! :) – Martin Mulder May 01 '13 at 09:31

1 Answers1

6

Lambdas can contain more than one line, the syntax in this case is similar to anonymous delegates from C# 2.0:

Func<int, int> f = i =>
{
     Debug.WriteLine("Inside the function!");
     return i + 1;
};
Dennis
  • 37,026
  • 10
  • 82
  • 150