委托关键字与lambda符号 [英] delegate keyword vs. lambda notation
本文介绍了委托关键字与lambda符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一旦编译,是否有区别:
delegate {x = 0; }
和
code>()=> {x = 0}
?
解决方案
简短答案:否。
更长的回答可能不相关:
- 如果您将lambda分配给委托类型(例如
Func
或Action
)你会得到一个匿名委托。 - 如果你将lambda分配给一个表达式类型,你会得到一个表达式树而不是匿名委托。表达式树可以被编译成匿名委托。
编辑:
这里是表达式的一些链接。 >
- System.Linq.Expression.Expression(TDelegate)(从这里开始)。
- 内存中的代理(如System.Func)使用 System.Linq.Enumerable 。使用表达式的Linq to SQL(和其他任何内容)使用 System.Linq。可查询。查看这些方法的参数。
- ScottGu的说明。简而言之,Linq内存中将产生一些匿名方法来解决您的查询。 Linq to SQL将生成一个表达式树,表示该查询,然后将该树转换为T-SQL。 Linq to Entities将产生一个表示树,表示该查询,然后将该树转换为平台适当的SQL。
Once it is compiled, is there a difference between:
delegate { x = 0; }
and
() => { x = 0 }
?
解决方案
Short answer : no.
Longer answer that may not be relevant:
- If you assign the lambda to a delegate type (such as
Func
orAction
) you'll get an anonymous delegate. - If you assign the lambda to an Expression type, you'll get an expression tree instead of a anonymous delegate. The expression tree can then be compiled to an anonymous delegate.
Edit: Here's some links for Expressions.
- System.Linq.Expression.Expression(TDelegate) (start here).
- Linq in-memory with delegates (such as System.Func) uses System.Linq.Enumerable. Linq to SQL (and anything else) with expressions uses System.Linq.Queryable. Check out the parameters on those methods.
- An Explanation from ScottGu. In a nutshell, Linq in-memory will produce some anonymous methods to resolve your query. Linq to SQL will produce an expression tree that represents the query and then translate that tree into T-SQL. Linq to Entities will produce an expression tree that represents the query and then translate that tree into platform appropriate SQL.
这篇关于委托关键字与lambda符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文