C# 是单调度语言还是多调度语言?
我试图准确地理解什么是单次调度和多次调度。
我刚刚读过这个:
http://en.wikipedia.org/wiki/Multiple_dispatch
从这个定义来看,在我看来即使在编译时选择要调用的重载,C# 和 VB.Net 也是多重分派的。
我在这里是正确的,还是我错过了什么? 谢谢!
I'm trying to understand what single and multiple dispatch are, exactly.
I just read this:
http://en.wikipedia.org/wiki/Multiple_dispatch
And from that definition is seems to me that C# and VB.Net are multiple-dispatch, even though the choice of which overload to call is made at compile-time.
Am I correct here, or am I missing something?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我知道这是一个老问题。
在 .Net 4.0 中,您可以使用
dynamic
关键字来实现多种方法...请看以下示例 .Net 4.0 用于重构现有“if”的优化代码条件和“is”运算符I understand that this is an old question..
In .Net 4.0 you can use
dynamic
keyword for multi methods... Take a look at the following for an example .Net 4.0 Optimized code for refactoring existing "if" conditions and "is" operatorGoF 访问者模式是如何进行双重调度的示例。 Scott Meyers“更有效的 C++”向您展示了如何在 C++ 中做到这一点。 这是来自 Dobbs 博士的链接,其中讨论了如何在 Java 和 C++ 中进行双重调度。
The GoF Visitor Pattern is an example of how to do double dispatch. Scott Meyers "More Effective C++" shows you how to do it in C++. Here's a link from Dr Dobbs that talks about how to do double dispatch in both Java and C++.
根据引用的维基百科文章,多重调度,由定义基于所涉及对象的运行时类型,因此 C# 和 VB.net 不使用它,因为正如您所说,决定是在编译时做出的。
According to the cited Wikipedia article, multiple dispatch, by definition, is based on the runtime types of the objects involved, so C# and VB.net don't use it, because the decision is made, as you state, at compile-time.
C# 不支持多重调度。 访问者设计模式模拟了可以描述为多重调度的东西,尽管访问者模式主要关注将算法与层次结构分开。
C# does not support multiple dispatch. The Visitor Design pattern emulates something that could be described as multiple dispatch, even though the Visitor pattern's mainly focus on separate the algorithm from an hierarchy.
也许有人会对使用动态关键字的多次调度的 C# 示例感兴趣 (MSDN 博客)
我们可以创建同一方法的多个重载,根据其参数类型的不同组合进行专门化:
现在神奇的部分:
这是有效的,因为“asdynamic”强制转换,它告诉 C# 编译器,而不是仅仅调用 ReactSpecialization(Animal, Animal) 来动态检查每个参数的类型,并在运行时选择要调用的方法重载。
为了证明它确实有效:
输出:
维基百科说 C# 4.0(动态)是“多重调度”语言。
我还认为 Java、C#(4.0 之前)、C++ 等语言都是单调度的。
Maybe somebody will be interested in good C# example for multiple dispatch using dynamic keyword (MSDN blog)
We can create several overloads of the same method, specialized according to different combinations of their parameter types:
And now the magic part:
This works because of the "as dynamic" cast, which tells the C# compiler, rather than just calling ReactSpecialization(Animal, Animal), to dynamically examine the type of each parameter and make a runtime choice about which method overload to invoke.
To prove it really works:
Output:
Wikipedia says that C# 4.0 (dynamic) is "multiple dispatch" language.
I also think that languages such as Java, C# (prior to 4.0), C++ are single dispatch.
C# 是单一调度,但有一些博客文章的标题看起来像是在尝试模拟多方法。 如果我可以加载其中一篇文章,我将在这里更新我的答案。
C# is single dispatch but there are some blog posts which by their title looks like they are trying to emulate multimethods. If I can get one of the articles to load I will update my answer here.
对于那些使用搜索引擎查找本文的人,C# 4.0 引入了动态 关键字。 代码如下所示。
For those that find this article using a search engine, C# 4.0 introduces the dynamic keyword. The code would look like the following.
好的,我理解函数重载与多重分派之间的细微差别。
基本上,区别在于是否在运行时或编译时选择调用哪个方法。 现在,我知道每个人都这么说过,但如果没有一个明确的例子,这听起来非常明显,因为 C# 是静态类型的,而多调度语言(至少对我来说)似乎是动态类型的。 到目前为止,通过这个定义,多重分派和函数重载对我来说听起来完全一样。
真正产生区别的情况是,当您
CaptureSpaceShip(IRebelAllianceShip Ship)
和CaptureSpaceShip(Xwing Ship)
>IRebelAllianceShip
和CaptureSpaceShip
)是多态的,并且完整示例:
XWing 显然实现了 IRebelAllianceShip。
在这种情况下,将调用第一个方法,而如果 C# 实现多重调度,则将调用第二个方法。
对于文档重新散列感到抱歉...在我看来,这似乎是解释这种差异的最清晰方法,而不是仅仅阅读每个调度方法的定义。
更正式的解释:
http:/ /en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_is_more_than_function_overloading
OK, I understood the subtle difference where function overloading is different from multiple-dispatch.
Basically, the difference is whether which method to call is chosen at run-time or compile-time. Now, I know everybody's said this, but without a clear example this sounds VERY obvious, given that C# is statically typed and multiple-dispatch languages (apparently to me, at least) seem to be dynamically typed. Up to now, with just that definition multiple-dispatch and function overloading sounded exactly the same to me.
The case where this makes a real difference is when you
CaptureSpaceShip(IRebelAllianceShip ship)
andCaptureSpaceShip(Xwing ship)
IRebelAllianceShip
andCaptureSpaceShip
) are polymorphic, andFull Example:
XWing obviously implements IRebelAllianceShip.
In this case, the first method will be called, whereas if C# implemented multiple-dispatch, the second method would be called.
Sorry about the doc rehash... This seems to me the clearest way to explain this difference, rather than just reading the definitions for each dispatch method.
For a more formal explanation:
http://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_is_more_than_function_overloading