C# 是单调度语言还是多调度语言?

发布于 2024-07-12 22:15:17 字数 271 浏览 12 评论 0原文

我试图准确地理解什么是单次调度和多次调度。

我刚刚读过这个:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

这个俗人 2024-07-19 22:15:33

我知道这是一个老问题。

在 .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" operator

指尖凝香 2024-07-19 22:15:32

GoF 访问者模式是如何进行双重调度的示例。 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++.

空心↖ 2024-07-19 22:15:30

根据引用的维基百科文章,多重调度,由定义基于所涉及对象的运行时类型,因此 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.

素手挽清风 2024-07-19 22:15:29

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.

南城追梦 2024-07-19 22:15:28

也许有人会对使用动态关键字的多次调度的 C# 示例感兴趣 (MSDN 博客

class Animal 
{ 
}

class Cat : Animal 
{ 
}

class Dog : Animal 
{ 
}

class Mouse : Animal 
{ 
}

我们可以创建同一方法的多个重载,根据其参数类型的不同组合进行专门化:

void ReactSpecialization(Animal me, Animal other) 
{ 
    Console.WriteLine("{0} is not interested in {1}.", me, other); 
}

void ReactSpecialization(Cat me, Dog other) 
{ 
    Console.WriteLine("Cat runs away from dog."); 
}

void ReactSpecialization(Cat me, Mouse other) 
{ 
    Console.WriteLine("Cat chases mouse."); 
}

void ReactSpecialization(Dog me, Cat other) 
{ 
    Console.WriteLine("Dog chases cat."); 
}

现在神奇的部分:

void React(Animal me, Animal other) 
{ 
    ReactSpecialization(me as dynamic, other as dynamic); 
}

这是有效的,因为“asdynamic”强制转换,它告诉 C# 编译器,而不是仅仅调用 ReactSpecialization(Animal, Animal) 来动态检查每个参数的类型,并在运行时选择要调用的方法重载。

为了证明它确实有效:

void Test() 
{ 
    Animal cat = new Cat(); 
    Animal dog = new Dog(); 
    Animal mouse = new Mouse();

    React(cat, dog); 
    React(cat, mouse); 
    React(dog, cat); 
    React(dog, mouse); 
}

输出:

Cat runs away from dog.
Cat chases mouse.
Dog chases cat.
Dog is not interested in Mouse.

维基百科说 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)

class Animal 
{ 
}

class Cat : Animal 
{ 
}

class Dog : Animal 
{ 
}

class Mouse : Animal 
{ 
}

We can create several overloads of the same method, specialized according to different combinations of their parameter types:

void ReactSpecialization(Animal me, Animal other) 
{ 
    Console.WriteLine("{0} is not interested in {1}.", me, other); 
}

void ReactSpecialization(Cat me, Dog other) 
{ 
    Console.WriteLine("Cat runs away from dog."); 
}

void ReactSpecialization(Cat me, Mouse other) 
{ 
    Console.WriteLine("Cat chases mouse."); 
}

void ReactSpecialization(Dog me, Cat other) 
{ 
    Console.WriteLine("Dog chases cat."); 
}

And now the magic part:

void React(Animal me, Animal other) 
{ 
    ReactSpecialization(me as dynamic, other as dynamic); 
}

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:

void Test() 
{ 
    Animal cat = new Cat(); 
    Animal dog = new Dog(); 
    Animal mouse = new Mouse();

    React(cat, dog); 
    React(cat, mouse); 
    React(dog, cat); 
    React(dog, mouse); 
}

Output:

Cat runs away from dog.
Cat chases mouse.
Dog chases cat.
Dog is not interested in Mouse.

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.

尤怨 2024-07-19 22:15:27

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.

你的心境我的脸 2024-07-19 22:15:25

对于那些使用搜索引擎查找本文的人,C# 4.0 引入了动态 关键字。 代码如下所示。

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

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

For those that find this article using a search engine, C# 4.0 introduces the dynamic keyword. The code would look like the following.

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

void Main() {   
    IRebelAllianceShip theShip = new XWing();  
    CaptureSpaceShip((dynamic)theShip);
}
坚持沉默 2024-07-19 22:15:24

好的,我理解函数重载与多重分派之间的细微差别。

基本上,区别在于是否在运行时或编译时选择调用哪个方法。 现在,我知道每个人都这么说过,但如果没有一个明确的例子,这听起来非常明显,因为 C# 是静态类型的,而多调度语言(至少对我来说)似乎是动态类型的。 到目前为止,通过这个定义,多重分派和函数重载对我来说听起来完全一样。

真正产生区别的情况是,当您

  • 有两个参数类型不同的方法重载时(CaptureSpaceShip(IRebelAllianceShip Ship)CaptureSpaceShip(Xwing Ship) >
  • 两种类型(IRebelAllianceShipCaptureSpaceShip)是多态的,并且
  • 您使用声明为较高类型的引用调用该方法,该引用实际上指向较低类型的对象

完整示例:

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

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

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

  • have two overloads of a method that differ on the type of a parameter (CaptureSpaceShip(IRebelAllianceShip ship) and CaptureSpaceShip(Xwing ship)
  • the two types (IRebelAllianceShip and CaptureSpaceShip) are polymorphic, and
  • you call the method with a reference declared as the higher type, which actually points to an object of the lower type

Full Example:

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文