是什么在F#头等舱值在C#中的代表和功能之间的区别? [英] What is the difference between delegates in C# and functions as first class values in F#?

查看:141
本文介绍了是什么在F#头等舱值在C#中的代表和功能之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更具体有哪些特点(如果有的话)代表有功能的F#头等舱值没有;并且是什么函数作为第一类值具有的特性(如果有的话),在C#中的代表没有?

More specifically what are the characteristics (if any) that delegates have that functions as first class values in F# don't have; and what are the characteristics that functions as first class values have (if any) that delegates in C# don't have?

推荐答案

代表和F#一流的函数值是完全不同的。

Delegates and F# "First class function values" are quite different.

代表是CLR,围绕函数指针+对象对类型安全包装的机制(实例方法,在这个终场前被连同方法捕获地址)。

Delegates are a mechanism of the CLR, a type-safe wrapper around function-pointer+object pairs (for instance methods, the this-pointer gets captured together with the method address).

F#的功能,另一方面价值观,是实现抽象类的 FSharpFunc<,> (它使用被称为 FastFunc<,> F#的正式发布之前)。调用经由普通的虚拟方法,这是比代表调用快得多发生。
这是F# - 团队并没有在第一时间使用委托的原因。

F# function values on the other hand, are implementation of an abstract class FSharpFunc<,> (it used to be called FastFunc<,> before the official release of F#). Invocation happens via ordinary virtual methods, which is much faster than delegate invocation. That is the reason the F#-team didn't use delegates in the first place.

所以,如果你能实现功能通过第一类值抽象类/虚拟方法,为什么微软添加代理?

So if you can "implement" functions as first class values via abstract classes/virtual methods, why did Microsoft add delegates?


  • 有没有替代在.NET 1.0 / 1.1,没有仿制药,所以你必须定义一个新的委托类型(=功能型)为您想使用的每个函数签名。

  • (没有,只是在Java中使用接口,如不计。:-P)

  • There was no alternative In .NET 1.0/1.1, there were no generics, so you had to define a new delegate type (="function type") for every function signature you wanted to use.
  • (No, just using interfaces like in Java doesn't count. :-P )

好吧,但我们自.NET 2.0泛型,为什么我们仍然有代表?为什么我们不能只使用 Func键<,> 动作<> 的一切

Ok, but we have Generics since .NET 2.0, why do we still have delegates? Why can't we just use Func<,> and Action<> for everything?


  • 向后兼容性

  • 多路广播委托代表可以链接起来,形成新的代表。这种机制被用于实现在VB.NET和C#的事件。在幕后,事件其实只是一个代表领域。使用 + = 语法,你基本上是你的事件处理程序,委托添加到事件现场代表的链条。

  • Backwards compatibility
  • Multicast Delegates Delegates can be chained together to form new delegates. This mechanism is used to implement events in VB.NET and C#. Behind the scenes, an event is really just a single delegate field. Using the += syntax you essentially add your event-handler-delegate to the chain of delegates in the event field.

除了事件,有没有理由使用FSharpFunc LT超过代表&;,>

Apart from events, is there a reason to use delegates over FSharpFunc<,>

是的,有: FSharpFunc<的每个实施;,> ,包含的lambda表达式*,是一个新的类。而在.NET类编码在编译的程序集的元数据。在另一方面代表不需要额外的元数据。委托的​​类型的做,但的实例化的这些委托类型是元数据方面的自由。

Yes, one: Each and every implementation of FSharpFunc<,>, that includes lambda-expressions*, is a new class. And in .NET classes are encoded in the metadata of the compiled assembly. Delegates on the other hand require no extra metadata. The delegate types do but instantiating these delegate types is free in terms of metadata.

别急,是不是C#的lambda表达式/匿名方法为隐藏类也实现?

是的,C#lambda表达式采取最差的两个世界^ ^

Yes, C# lambdas take the worst of both worlds ^^

这篇关于是什么在F#头等舱值在C#中的代表和功能之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆