委托关键字与拉姆达符号拉姆、符号、关键字

2023-09-02 20:41:39 作者:眺望未来

一旦被编译,有没有之间的差异:

Once it is compiled, is there a difference between:

delegate { x = 0; }

() => { x = 0 }

推荐答案

简短的回答:没有

更长的答案可能不相关:

Longer answer that may not be relevant:

如果您分配lambda来委托类型(如 Func键动作),你会得到一个匿名委托。 如果您分配lambda来防爆pression类型,你会得到一个前pression树,而不是匿名委托。这位前pression树可以被编译为一个匿名委托。 If you assign the lambda to a delegate type (such as Func or Action) 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.

编辑: 下面是一些链接,防爆pressions。

Here's some links for Expressions.

System.Linq.Ex$p$pssion.Ex$p$pssion(TDelegate) (从这里开始)。 的Linq内存与代表(如System.Func)使用System.Linq.Enumerable. LINQ到SQL(和其他东西)与前pressions使用System.Linq.Queryable.看看这些方法的参数。 的Explanation从ScottGu 。概括地说,LINQ的内存会产生一些匿名的方法来解决您的查询。 LINQ到SQL会产生重新presents查询,然后把这种树为T-SQL的前pression树。 LINQ到实体将产生重新presents查询的前pression树,然后把这种树为平台的适当的SQL。 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.