是C#的一次调度或多个调度语言? [英] Is C# a single dispatch or multiple dispatch language?

查看:313
本文介绍了是C#的一次调度或多个调度语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解单个和多个调度是的,没错。

I'm trying to understand what single and multiple dispatch are, exactly.

我刚刚看了这样的:

<一href=\"http://en.wikipedia.org/wiki/Multiple_dispatch\">http://en.wikipedia.org/wiki/Multiple_dispatch

和来自定义在我看来,C#和VB.Net是多重调度,即使选择超载,其中调用在编译时作出。

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!

推荐答案

OK,我明白了微妙的区别在哪里函数重载是从多个分派不同的。

OK, I understood the subtle difference where function overloading is different from multiple-dispatch.

基本上,所不同的是选择在运行时或编译时是否要调用哪个方法。现在,我知道每个人的说这一点,但没有一个明显的例子,这听起来很明显的,因为C#是静态类型和多语言派遣(显然对我来说,至少)似乎是动态类型。截至目前,只有这个定义多重调度和函数重载听起来完全一样给我。

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.

在哪里这使得真正的区别的情况是,当你有2个重载其中一个参数的类型不同的方法,但2类型是多态的,和你声明为较高类型的引用,具有来电下类型的对象...
(如果有人能想到更好的办法来恩preSS这个,请随时编辑此答案)

The case where this makes a real difference is when you have 2 overloads of a method which differ on the type of a parameter, but the 2 types are polymorphic, and you call with a reference declared as the higher type, which has an object of the lower type... (If someone can think of a better way to express this, please feel free to edit this answer)

例如:

int CaptureSpaceShip(IRebelAllianceShip ship) {}
int CaptureSpaceShip(XWing ship) {}

void Main() { 
  IRebelAllianceShip theShip = new XWing();
  CaptureSpaceShip(theShip);
}

X翼战机显然实现IRebelAllianceShip。
在这种情况下,第一方法将被调用,而如果C#实现多调度,第二种方法将被称为

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.

有关一个更正式的解释:
<一href=\"http://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_is_more_than_function_overloading\">http://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_is_more_than_function_overloading

For a more formal explanation: http://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_is_more_than_function_overloading

这篇关于是C#的一次调度或多个调度语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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