Thursday, April 21, 2011

overloads

In the MS doc "New features in C# 4.0" there is this example
of applying "Betterness rules" to overload resolution...

Given these overloads of M..

M(string s, int i = 1);
M(object o);
M(int i, string s = “Hello”);
M(int i);

which does M(5) use?

Given these overloads, we can see the working of the betterness rules. M(string,int) is not applicable because 5 doesn’t convert to string. M(int,string) is applicable because its second parameter is optional, and so, obviously are M(object) and M(int).
M(int,string) and M(int) are both better than M(object) because the conversion from 5 to int is better than the conversion from 5 to object.
Finally M(int) is better than M(int,string) because no optional arguments are omitted.
Thus the method that gets called is M(int).

Ok, clear enough for this example...but I hope I don't have to debug code
that uses this feature a lot!



отсюда

No comments: