Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expression syntax

Tags:

c#

lambda

linq

Is it mandatory that lambda expression need to use when LINQ will be used, or are lambda expressions optional?

In lambda expressions, the sign => is always used. What does it mean?

 customers.Where(c => c.City == "London"); 

Here c => is used but why? What kind of meaning of using c =>. Please discuss in detail because I don't know lambda.

like image 895
Thomas Avatar asked Feb 22 '11 18:02

Thomas


3 Answers

No, you don't have to use a lambda expression. For example, your Where example could be written as:

private static bool IsLondon(Customer customer)
{
    return customer.City == "London";
}

...

var londoners = customers.Where(IsLondon);

That's assuming LINQ to Objects, of course. For LINQ to SQL etc, you'd need to build an expression tree.

As to why "=>" is always used in a lambda expression, that's simply because that's the way the operator is written - it's like asking why "+" is used for addition.

A lambda expression of "c => ..." is effectively giving the lambda expression a parameter called c... in this case generic type inference provides the type of c. The body provides either an action to perform or some calculation to return a value based on c.

A full-blown description of lambda expressions is beyond the scope of this answer. As a blatant plug for my book, however, they're covered in detail in chapter 9 of C# in Depth.

like image 99
Jon Skeet Avatar answered Nov 15 '22 16:11

Jon Skeet


The lambda expression

c => c.City == "London"

is shorthand for something like

bool IsCustomerInLondon(Customer c)
{
  return (c.City == "London");
}

It's just a very concise way of writing a simple function that returns a value. It's called an "anonymous function" because it's never assigned a name or a formal definition (the parameter types and the return type are inferred from the context).

(Actually, it's not just shorthand; lambda expressions are related to some other constructs called closures, which are very cool and powerful tools.)

like image 28
Jeremy Todd Avatar answered Nov 15 '22 18:11

Jeremy Todd


Think about lambdas as anonymous of functions. I'll try to explain it with following code.

public bool IsLondon(Customer customer)
{
    return customer.City == "London";
}

Now we strip down function name and get rid of braces:

public bool Customer customer    
    return customer.City == "London";

We don't need return type, because compiler is able to deduce type from expression:

 customer.City == "London";

In the same manner compiler knows about input type, so we don't need to specify it.

So basically, what we left with is

customer
   return customer.City == "London";

And lambda syntax in c# is basically:

(parameter list) => "expression"

You can also write "multi-line" expressions, but then you have to surround your code with curly braces. Also you will need to use "return" statement, like you usually do.

like image 28
ionoy Avatar answered Nov 15 '22 18:11

ionoy