10

I am new to this LINQ field and one thing am trying to do.

I have an action delegate(written below) which i want to convert in lambda expression.

      Action<string> custom = delegate(string name)
            {
                lstCutomers.Add(new Customer(name, coutries[cnt]));
                name = name + " Object Created";
            };

What will be the lambda expression for same. I just want to know that can i write multiple statements in lambda if no then Why?

Thanks in advance.

George Duckett
  • 31,770
  • 9
  • 95
  • 162
D J
  • 6,908
  • 13
  • 43
  • 75
  • yes you can write a single statement or a block of statement – Peeyush Jan 07 '12 at 09:10
  • Check out: http://stackoverflow.com/questions/299703/delegate-keyword-vs-lambda-notation for a detailled discussion about the delegate notation Vs Lambda notation. – Christophe Geers Jan 07 '12 at 09:12
  • You realize that assigning a value back to `name` isn't going to have any effect, right? So your second statement is irrelevant, and you can use a non-statement lambda. Either you've given a bad example, or you've probably got a bug in your code... – Jon Skeet Jan 07 '12 at 09:15

1 Answers1

19

You can't create a lambda expression, since you're not returning anything. You can however create a statement lambda:

Action<string> custom = (name) =>
        {
            lstCutomers.Add(new Customer(name, coutries[cnt]));
            name = name + " Object Created";
        };
George Duckett
  • 31,770
  • 9
  • 95
  • 162
  • that would be the anonymous method. It should be something like Action customLambda = (name) => lstCutomers.Add(new Customer(name, coutries[cnt]);name = name + " Object Created"; ); Which is not compiling even. – D J Jan 07 '12 at 09:03
  • Not sure then, as `Expression`'s always end up with a value. I thought the `=>` made it a lambda expression (which is converted to an anonymous method because of the type of custom). – George Duckett Jan 07 '12 at 09:05
  • 1
    @DJ: From [MSDN](http://msdn.microsoft.com/en-us/library/bb397687.aspx) `A lambda expression is an anonymous function that can contain expressions and statements` also, see the section on Statement Lambdas. Have updated my answer. – George Duckett Jan 07 '12 at 09:07
  • so it can contain multiple statements as well but i cant see any example of it.. do u have any where one lamda expressions has multiple statements? – D J Jan 07 '12 at 09:11
  • See the first example in the Variable Scope in Lambda Expressions heading on the MSDN page I linked to. The syntax is the same as a statement lambda, except you return something. – George Duckett Jan 07 '12 at 09:13
  • @DJ: It's fairly common in things like [`Parallel.ForEach`](http://msdn.microsoft.com/en-us/library/dd992001.aspx) – Jon Skeet Jan 07 '12 at 09:14
  • @JonSkeet: Except those are *statement* lambdas, since they wouldn't return anything. – George Duckett Jan 07 '12 at 09:17
  • 1
    @GeorgeDuckett: I was answering the OP's question about where one might use a lambda expression with multiple statements. Also, not returning anything is orthogonal to being a statement lambda: you can have statement lambdas which return values, and non-statement lambdas which don't. – Jon Skeet Jan 07 '12 at 09:20
  • 3
    @GeorgeDuckett: Note that in your first comment, you're talking about `Expression`, which is only used when converting a lambda expression into an expression tree. That's irrelevant here. Both statement lambdas and non-statement lambdas are lambda expressions. (The spec doesn't actually use the term "statement lambda" - it talks about the lambda expressions with a body which is either an expression or a statement body.) – Jon Skeet Jan 07 '12 at 09:21
  • @JonSkeet: Ahh ok, didn't realise you could get statement lambdas that return things and visa-versa. Thanks for the corrections. :) – George Duckett Jan 07 '12 at 09:21