委托关键字与 lambda 表示法

2

本文介绍了委托关键字与 lambda 表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

一经编译,有没有区别:

Once it is compiled, is there a difference between:

delegate { x = 0; }

() => { x = 0 }

?

推荐答案

简答:没有.

可能不相关的较长答案:

Longer answer that may not be relevant:

  • 如果将 lambda 分配给委托类型(例如 FuncAction),您将获得匿名委托.
  • 如果将 lambda 分配给 Expression 类型,您将获得表达式树而不是匿名委托.然后可以将表达式树编译为匿名委托.
  • 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.

这是表达式的一些链接.

Here's some links for Expressions.

  • System.Linq.Expression.Expression(TDelegate)(开始在这里).
  • Linq in-memory with delegates(例如 System.Func)使用 System.Linq.Enumerable.带有表达式的 Linq to SQL(和其他任何东西)使用 System.Linq.可查询.查看这些方法的参数.
  • 一个解释来自 ScottGu.简而言之,Linq in-memory 会产生一些匿名方法来解决您的查询.Linq to SQL 将生成一个表示查询的表达式树,然后将该树转换为 T-SQL.Linq to Entities 将生成一个表示查询的表达式树,然后将该树转换为适合平台的 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.

这篇关于委托关键字与 lambda 表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14