7

From the article Anders Hejsberg interview, "the way we do overload resolution in C# is different from any other language" Can somebody provide some examples with C# and Java?

6
  • 1
    I would start at Eric Lippert's blog. There are 8 posts marked taged with overload resolution. Commented May 29, 2010 at 3:36
  • Why do you need an example? In Java you can easily replace a function in a subclass, in C# you mark the function as virtual, then can override it. Commented May 29, 2010 at 3:37
  • @R0MANARMY: That's the correct answer to this question. If you post it as an answer, I will upvote it. Commented May 29, 2010 at 3:37
  • 4
    @James: That is overriding, not overloading. Commented May 29, 2010 at 3:38
  • 1
    @Robert Harvey: I feel like that's kind of a half-assed answer. There are quite a few people on SO that could answer the question in much better detail than "read X." Content of said answers would also be more valuable to SO. Commented May 29, 2010 at 3:41

2 Answers 2

19

What Anders was getting at here was that the original design team explicitly designed the overload resolution algorithm to have certain properties that worked nicely with versioning scenarios, even though those properties seem backwards or confusing when you consider the scenarios without versioning.

Probably the most common example of that is the rule in C# that if any method on a more-derived class is an applicable candidate, it is automatically better than any method on a less-derived class, even if the less-derived method has a better signature match. This rule is not, to my knowledge, found in other languages that have overload resolution. It seems counterintuitive; if there's a method that is a better signature match, why not choose it? The reason is because the method that is a better signature match might have been added in a later version and thereby be introducing a "brittle base class" failure.

For more thoughts on how various languages handle brittle base class failures, see

Link

and for more thoughts on overload resolution, see

Link

Sign up to request clarification or add additional context in comments.

2 Comments

Do you get paged whenever your name is mentioned in a question to make sure it's answered regardless of time of day ;) ?
C++ automatically hides all functions with the same name, if one is defined in a more derived class. So unless you use a "using base::functionname" declaration (which "unhides" the name again), the effect is similar. It's not exactly the same though, because in C++ those hidden names will not be considered at all, even if no overload in the more derived class is applicable. It serves the same prupose though - new functions in base classes will not be called unintentionally.
1

The way that C# handles overloading from an internal perspective is what's different.

The complete quote from Anders:

I have always described myself as a pragmatic guy. It's funny, because versioning ended up being one of the pillars of our language design. It shows up in how you override virtual methods in C#. Also, the way we do overload resolution in C# is different from any other language I know of, for reasons of versioning. Whenever we looked at designing a particular feature, we would always cross check with versioning. We would ask, "How does versioning change this? How does this function from a versioning perspective?" It turns out that most language design before has given very little thought to that.

1 Comment

Well....that explains his other question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.