Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expression in C# [duplicate]

Tags:

c#

.net

Is there a situation where the use of the lambda expression is particularly helpful or its mainly usage is to write less code?

like image 448
Maurizio Reginelli Avatar asked Feb 27 '10 15:02

Maurizio Reginelli


People also ask

Does C have lambda expression?

No, C doesn't have lambda expressions (or any other way to create closures). This is likely so because C is a low-level language that avoids features that might have bad performance and/or make the language or run-time system more complex.

What is a lambda function in C?

In C++11 and later, a lambda expression—often called a lambda—is a convenient way of defining an anonymous function object (a closure) right at the location where it's invoked or passed as an argument to a function.

What is lambda expression?

A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

What is a lambda expression example?

Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces.


1 Answers

The justification for adding lambdas to the language was two things.

(1) They make syntactic transformation of query comprehensions possible. When you say

from customer in customers 
where customer.City == "London" 
select customer

That becomes

customers.Where(customer=>customer.City == "London")

(2) They can be turned into expression trees, and thereby make LINQ-to-SQL, LINQ-to-Entities, and so on, possible. That is, they can represent both the ability to do their semantics and the ability to inspect their structure.

like image 171
Eric Lippert Avatar answered Oct 22 '22 17:10

Eric Lippert