Is C# a single dispatch or multiple dispatch langu

2019-01-17 03:12发布

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!

8条回答
Luminary・发光体
2楼-- · 2019-01-17 03:46

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.

查看更多
Rolldiameter
3楼-- · 2019-01-17 03:50

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);
}
查看更多
We Are One
4楼-- · 2019-01-17 03:51

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.

查看更多
Luminary・发光体
5楼-- · 2019-01-17 03:54

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

查看更多
该账号已被封号
6楼-- · 2019-01-17 03:55

Maybe somebody will be interested in good C# example for multiple dispatch using dynamic keyword (MSDN blog)

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.

查看更多
我只想做你的唯一
7楼-- · 2019-01-17 04:05

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

查看更多
登录 后发表回答