Is C# a single dispatch or multiple dispatch language?

后端 未结 8 1950
悲&欢浪女
悲&欢浪女 2020-12-02 20:25

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

相关标签:
8条回答
  • 2020-12-02 20:55

    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.

    0 讨论(0)
  • 2020-12-02 20:58

    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 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)

    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

    0 讨论(0)
  • 2020-12-02 20:58

    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.

    0 讨论(0)
  • 2020-12-02 20:58

    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

    0 讨论(0)
  • 2020-12-02 21:02

    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.

    0 讨论(0)
  • 2020-12-02 21:02

    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++.

    0 讨论(0)
提交回复
热议问题