委托、Lambda、Action、Func、匿名函数 [英] Delegates, Lambdas, Action, Func, Anonymous Functions
问题描述
我只是想验证我对以下内容的理解
I just want to verify my understanding about the following
- 委托 - 方法签名
- Lambdas - 匿名函数
- 匿名函数 - 仅此而已
- Action - 一个不返回任何内容的匿名函数
- Func - 一个返回某些东西的匿名函数
- Delegate - a method signature
- Lambdas - anonymous function
- Anonymous Function - just that
- Action - An anonymous function that returns nothing
- Func - An anonymous function that returns something
嗯...他们都做类似的事情,你如何定义&知道什么时候使用每个?
hmm... they all do similar things, how do you define & know when to use each?
对不起,我解释得不好
推荐答案
委托 - 它不是方法签名.
是一种封装了方法的类型
.因此,委托声明应该有一个类似于它想要封装的方法的签名.何时使用 Delegate - 每当您想将方法传递给另一个函数时.
有关更多信息,请参阅 this 和 这个.Delegate - it is not a method signature.
It is a type which encapsulates a method
. Hence a delegate declaration should have a signature similar to the method it wants to encapsulate.When to use Delegate - whenever you want to pass a method to another function.
For more see this and this.Lambdas - 编写匿名函数的简写和更具表现力的方式.但还有更多.lambda 表达式也可以转换为表达式树.有关更多信息,请参阅this.
Lambdas - short hand and more expressive way of writing an anonymous function. But there is more to it. A lambda expression can also be converted to an expression tree. For more see this.
匿名函数 - 是的..就这样
Action - 它是一个委托,可以封装一个不返回任何内容的函数.所以你应该把它想象成
一种可以封装一个动作的类型
,并在你需要传递一个动作时使用它.Action - It is a delegate which can encapsulate a function that returns nothing. So you should think of it as
a type that can encapsulate an action
and use it when you need to pass an action around.Func - 可以封装返回某些内容的函数的委托.但是您应该将其视为
一种可以封装转换的类型
,并在您想要传递转换时使用.Func - A delegate that can encapsulate a function that returns something. But you should look at it as
a type that can encapsulate a transformation
and use when you want to pass around a transformation.这篇关于委托、Lambda、Action、Func、匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!