开发者

Lambda expression in C# [duplicate]

This question already has answers here: C# Lambda expressions: Why should I use them? (16 answers) 开发者_StackOverflow Closed 8 years ago.

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


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.


Lambda expressions are syntactic sugar for anonymous methods, and their use cases are mostly the same.

Lambdas can also be converted to expression trees.

As they are much shorter and easier to write (at least for the simple cases), that in itself is helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜