32

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!

Maniero
  • 10,311
  • 6
  • 40
  • 85
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243

8 Answers8

50

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

emilaz
  • 1,722
  • 1
  • 15
  • 31
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
  • This is a nice example of the major difference. – Paul Stovell Feb 20 '09 at 05:06
  • Thanks! I needed an example like this. – John Mar 02 '09 at 17:59
  • 1
    @Daniel Magliola, please note dynamic dispatch is now possible in C# 4.0 by utilising dynamic – RichardOD Oct 25 '11 at 16:33
  • 1
    "multiple-dispatch languages seem to be dynamically typed". After reading http://www.perl.com/pub/2007/12/06/soto-11.html?page=1, which is how I got to this question, I think it's the opposite, actually (see the part where he says "the main reason to put types into Perl 6 turns out not to be strong typing, but rather multiple dispatch"). If you're going to decide what method to call based on the arguments types, you probably need to have a method definition with explicit types, which is something dynamically-typed languages don't usually have (at least the languages I've seen). – dsetton Apr 05 '12 at 10:20
  • @David Mulfrod's answer (the new dynamic keyword) should be the correct answere - namely, if you want then you can let CLR do dynamic dispatch (I guess JVM's invoke-dynamic is similar, but it's not available for Java) – Ustaman Sangat Apr 09 '13 at 15:44
  • To clarify: `CaptureSpaceShip((IRebelAllianceShip)theShip)` calls the `IRebelAllianceShip` overload (independently of the value of `theShip`), but here the cast is unnecessary because the variable has already been statically declared as `IRebelAllianceShip`. `CaptureSpaceShip((XWing)theShip)` calls the XWing overload (or crashes, if `theShip` is not an `XWing`). In C# 4.0+, `CaptureSpaceShip((dynamic)theShip)` calls the most specific overload applicable (or crashes, if two applicable overloads are equally specific). – Bjartur Thorlacius Aug 29 '18 at 15:24
30

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);
}
David Mulford
  • 301
  • 3
  • 2
7

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.

JoshBerke
  • 66,142
  • 25
  • 126
  • 164
4

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.

Maxim
  • 13,029
  • 6
  • 30
  • 45
2

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.

Schildmeijer
  • 20,702
  • 12
  • 62
  • 79
1

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.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
0

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

Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
0

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

duffymo
  • 305,152
  • 44
  • 369
  • 561