Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# overloading resolution?

Tags:

c#

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?

like image 858
northTiger Avatar asked May 29 '10 03:05

northTiger


1 Answers

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

http://blogs.msdn.com/b/ericlippert/archive/tags/brittle+base+classes/

and for more thoughts on overload resolution, see

http://blogs.msdn.com/b/ericlippert/archive/tags/overload+resolution/

like image 187
Eric Lippert Avatar answered Oct 19 '22 03:10

Eric Lippert