简体   繁体   English

代表,Lambdas,Action,Func,匿名函数

[英]Delegates, Lambdas, Action, Func, Anonymous Functions

I just want to verify my understanding about the following 我只想验证我对以下内容的理解

  • Delegate - a method signature 委托 - 方法签名
  • Lambdas - anonymous function Lambdas - 匿名函数
  • Anonymous Function - just that 匿名函数 - 就是这样
  • Action - An anonymous function that returns nothing 操作 - 不返回任何内容的匿名函数
  • Func - An anonymous function that returns something Func - 返回某些内容的匿名函数

hmm... they all do similar things, how do you define & know when to use each? 嗯...他们都做类似的事情,你如何定义和知道何时使用每个?

sorry, I don't explain well 对不起,我不好解释

  • Delegate - it is not a method signature. 委托 - 它不是方法签名。 It is a type which encapsulates a method . 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 - short hand and more expressive way of writing an anonymous function. Lambdas - 写一个匿名函数的简短手和更具表现力的方式。 But there is more to it. 但还有更多。 A lambda expression can also be converted to an expression tree. lambda表达式也可以转换为表达式树。 For more see this . 欲了解更多看到这个

  • Anonymous Function - yes .. just that 匿名函数 - 是的......就是这样

  • Action - It is a delegate which can encapsulate a function that returns nothing. Action - 它是一个委托,它可以封装一个不返回任何内容的函数。 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. 因此,您应该将其视为a type that can encapsulate an action并在需要传递操作时使用它。

  • Func - A delegate that can encapsulate a function that returns something. Func - 可以封装返回某些内容的函数的委托。 But you should look at it as a type that can encapsulate a transformation and use when you want to pass around a transformation. 但是您应该将其视为a type that can encapsulate a transformation并在您想要传递转换时使用。

Action and Func are just special cases of the Delegate . ActionFunc只是Delegate的特例。 Delegate itself is something that references a method and can be used to call it. 委托本身是引用方法并可用于调用它的东西。

Don't try to see C# + these features. 不要试图看到C#+这些功能。 Also because there is not a strict pragmatic answer to your question. 还因为对你的问题没有严格务实的答案。

Start from a functional programming point of view, for example try to learn F# to get inside what and when lambdas are used (actually everywhere inside a functional language) and then you'll understand things better. 从函数式编程的角度出发,例如尝试学习F#以获取内部使用lambdas的内容(实际上是函数式语言中的所有内容),然后您将更好地理解事物。

It will open your mind and make you think differently about imperative programming or mixed languages like C#. 它会打开你的思维,让你对命令式编程或C#等混合语言有不同的看法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM