473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Overloaded Method Resolution

Hi,

I even read the related parts of language specification
but could not find anything that explains the following:
====================
public class A {
public void Foo(object o) {
System.Console.WriteLine("Foo(object o)");
}

public void Foo(string s) {
System.Console.WriteLine("Foo(string s)");
}

public static void Main() {
A a = new A();
a.Foo(null);
}
}
===================
I expect to get ("Foo(object o)" but this prints ("Foo
(string s)"). Can anybody explain why? Which part of the
language specification explains this behavior?

Thanks,
Hamza GOLYERI
Nov 15 '05 #1
4 1141
Interestig. It appears that null is defined as a string. Perhaps
strange, but then again, it's not really an object either, and not really
a string (confused).

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
Morten Wennevik <Mo************@hotmail.com> wrote:
Interestig. It appears that null is defined as a string. Perhaps
strange, but then again, it's not really an object either, and not really
a string (confused).


No, the null literal is a value of the null type, which can be
implicitly converted to any reference type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Hamza GOLYERI <bi*******@hotmail.com> wrote:
I even read the related parts of language specification
but could not find anything that explains the following:
====================
public class A {
public void Foo(object o) {
System.Console.WriteLine("Foo(object o)");
}

public void Foo(string s) {
System.Console.WriteLine("Foo(string s)");
}

public static void Main() {
A a = new A();
a.Foo(null);
}
}
===================
I expect to get ("Foo(object o)" but this prints ("Foo
(string s)"). Can anybody explain why? Which part of the
language specification explains this behavior?


null can be implicitly converted to both string and object, so both
methods are candidates. Then from section 14.4.2 (ECMA numbering):

<quote>
Otherwise, the best function member is the one function member that is
better than all other function members with respect to the given
argument list, provided that each function member is compared to all
other function members using the rules in §14.4.2.2
</quote>

Now 14.4.2.2 says:

<quote>
Given an argument list A with a set of argument types A1, A2, ..., AN
and two applicable function members MP and MQ with parameter types P1,
P2, ..., PN and Q1, Q2, ..., QN, MP is defined to be a better function
member than MQ if

* for each argument, the implicit conversion from AX to PX is not
worse than the implicit conversion from AX to QX, and
* for at least one argument, the conversion from AX to PX is better
than the conversion from AX to QX.
</quote>

So, we've only got one argument here, so the two conversions in
question are from null to object and from null to string. Looking at
14.4.2.3, we have:

<quote>
Given an implicit conversion C1 that converts from a type S to a type
T1, and an implicit conversion C2 that converts from a type S to a type
T2, the better conversion of the two conversions is determined as
follows:

If T1 and T2 are the same type, neither conversion is better.
If S is T1, C1 is the better conversion.
If S is T2, C2 is the better conversion.
If an implicit conversion from T1 to T2 exists, and no implicit
conversion from T2 to T1 exists, C1 is the better conversion.
If an implicit conversion from T2 to T1 exists, and no implicit
conversion from T1 to T2 exists, C2 is the better conversion.
</quote>

Here S is the null type, T1 is object and T2 is string. (The rules are
symmetric, so you could choose them the other way round if you wanted.)

string and object aren't the same type, so the first rule doesn't
apply.

null isn't the same type as either string or object, so the second and
third rules don't apply.

There is no implicit conversion from object to string, so the fourth
rule doesn't apply.

There is an implicit conversion from string to object, but not vice
versa, so the fifth rule *does* apply, and the conversion from null to
string is better than the conversion from null to object - hence the
method which is chosen.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Great explanation! In summary since string is a "closer" conversion than
the base level Object it is picked by the runtime.

--
Eric Newton
C#/ASP Application Developer
er**@cc.ensoft-software.com [remove the first "CC."]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Hamza GOLYERI <bi*******@hotmail.com> wrote:
I even read the related parts of language specification
but could not find anything that explains the following:
====================
public class A {
public void Foo(object o) {
System.Console.WriteLine("Foo(object o)");
}

public void Foo(string s) {
System.Console.WriteLine("Foo(string s)");
}

public static void Main() {
A a = new A();
a.Foo(null);
}
}
===================
I expect to get ("Foo(object o)" but this prints ("Foo
(string s)"). Can anybody explain why? Which part of the
language specification explains this behavior?


null can be implicitly converted to both string and object, so both
methods are candidates. Then from section 14.4.2 (ECMA numbering):

<quote>
Otherwise, the best function member is the one function member that is
better than all other function members with respect to the given
argument list, provided that each function member is compared to all
other function members using the rules in §14.4.2.2
</quote>

Now 14.4.2.2 says:

<quote>
Given an argument list A with a set of argument types A1, A2, ..., AN
and two applicable function members MP and MQ with parameter types P1,
P2, ..., PN and Q1, Q2, ..., QN, MP is defined to be a better function
member than MQ if

* for each argument, the implicit conversion from AX to PX is not
worse than the implicit conversion from AX to QX, and
* for at least one argument, the conversion from AX to PX is better
than the conversion from AX to QX.
</quote>

So, we've only got one argument here, so the two conversions in
question are from null to object and from null to string. Looking at
14.4.2.3, we have:

<quote>
Given an implicit conversion C1 that converts from a type S to a type
T1, and an implicit conversion C2 that converts from a type S to a type
T2, the better conversion of the two conversions is determined as
follows:

If T1 and T2 are the same type, neither conversion is better.
If S is T1, C1 is the better conversion.
If S is T2, C2 is the better conversion.
If an implicit conversion from T1 to T2 exists, and no implicit
conversion from T2 to T1 exists, C1 is the better conversion.
If an implicit conversion from T2 to T1 exists, and no implicit
conversion from T1 to T2 exists, C2 is the better conversion.
</quote>

Here S is the null type, T1 is object and T2 is string. (The rules are
symmetric, so you could choose them the other way round if you wanted.)

string and object aren't the same type, so the first rule doesn't
apply.

null isn't the same type as either string or object, so the second and
third rules don't apply.

There is no implicit conversion from object to string, so the fourth
rule doesn't apply.

There is an implicit conversion from string to object, but not vice
versa, so the fifth rule *does* apply, and the conversion from null to
string is better than the conversion from null to object - hence the
method which is chosen.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Supra | last post by:
in vc.net :... g.DrawImage(imageList1.Images, new Rectangle(new Point(6,0),new Size(16,16))); i converted from vc.net to vb.net and i got error problem... ....overloaded resolution falied in...
20
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
5
by: Andreas Mueller | last post by:
Hi All, I have an overloaded generic method anmed "Foo": Class Test Class Xox(Of T) End Class Class Lulli(Of T) Inherits Xox(Of T) End Class
10
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name...
6
by: RobertEstelle | last post by:
Hello, I've been banging my head against this problem for a couple hours now to no avail. I'm trying to make a descendant of a class that has overloaded methods with variable argument lists....
1
by: mersinli | last post by:
Hi, I have got seven error and I dont find why compiler find error.. //commission class definition #ifndef COMMISSION_H #define COMMISSION_H #include <string> using std::string;
7
by: Random.Coder | last post by:
The output of this simple program below differs if it's compiled in Visual Studio 2005 and 2008. Is one the correct output, and if so, why? using System; namespace DerivedTestApp { class...
2
by: yuyang08 | last post by:
Dear all, I have a question on the const methods. If a method is overloaded with a const version, in the case that either one is okay (for example, the following code), which shall the compiler...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.