473,466 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wrong overload resolution ?

Hi guys,

Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!?

Am I right that C# compiler does wrong overload resolution ?

I've used parameters of type object and string here, just to illustrate the problem. Really I have a bit more deep inheritance graph, and the things get more interesting if the strongly typed overload is like override public void Method1(BaseType x). When I call it with parameter of type SubType (that inherits BaseType) the right method is called.

Thanks for any useful points.

Regadrs,
Vladimir Granitsky
using System;using System.Diagnostics;namespace OverloadResolution{ public class Class1 { virtual public void Method1(string s) { Trace.WriteLine("Class1.Method1"); } } public class Class2 : Class1 { override public void Method1(string s) { Trace.WriteLine("Class2.Method1a"); } public void Method1(object o) { Trace.WriteLine("Class2.Method1b"); } } class Client { [STAThread] static void Main(string[] args) { string s = "blah"; Class2 o2 = new Class2(); o2.Method1(s); } }}
Nov 17 '05 #1
13 2687
Hi Vladimir,

I work with .NET Framework 1.0 has the same "wrong" result!

Only change in "Main":
static void Main(string[] args)
{
string s = "blah";
Class1 o2 = new Class2();
o2.Method1(s);
}

or adding method into Class1, and "override" it in Class2

public virtual void Method1(object o) {
Console.WriteLine("Class1.Method1(object)");
}

gives me "right" result...

It looks like an overriden method has less "priority" than
a non-overriden methods in case of overload.
When i replaced method of Class2:
new public void Method1(string s)
{
Trace.WriteLine("Class2.Method1(string)");
}
then it was used by overload...

Strange and creepy :-(

Marcin
Hi guys,

Please, look at the code below and try to step into it. The compiled
code calls the loosely typed method public void Method1(object o) !?!?

Am I right that C# compiler does wrong overload resolution ?

I've used parameters of type object and string here, just to illustrate
the problem. Really I have a bit more deep inheritance graph, and the
things get more interesting if the strongly typed overload is
like override public void Method1(BaseType x). When I call it with
parameter of type SubType (that inherits BaseType) the right method is
called.

Thanks for any useful points.

Regadrs,
Vladimir Granitsky

using System;
using System.Diagnostics;
namespace OverloadResolution
{
public class Class1
{
virtual public void Method1(string s)
{
Trace.WriteLine("Class1.Method1");
}
}

public class Class2 : Class1
{
override public void Method1(string s)
{
Trace.WriteLine("Class2.Method1a");
}

public void Method1(object o)
{
Trace.WriteLine("Class2.Method1b");
}
}

class Client
{
[STAThread]
static void Main(string[] args)
{
string s = "blah";
Class2 o2 = new Class2();
o2.Method1(s);
}
}
}

Nov 17 '05 #2
> Am I right that C# compiler does wrong overload resolution ?

No.
If you introduce a new definition of an overloaded method then there is
a specific thing to do I cannot remember of right now but it is
documented.

--
----
http://michael.moreno.free.fr/

Nov 17 '05 #3
Well, I'm gonna guess that it's working according to the C# spec, but
I'm leaning toward it being a rather bad design choice on the spec writers.

To follow what going on, add the lines:
Class1 o1 = o2;
o1.Method1(s);

to the end of your Main() function. Run it, and you'll note that while
o2.Method1() give the wrong response, o1.Method1() is correct.

Now, replace the "override" with "new" (or just delete it). Now,
o2.Method1() is correct, while o1.Method1() is wrong.

So, what I THOUGHT was happening was that Method1(object) was hiding the
Class1.Method1() (including hiding it's override).
BUT, now change Method1(object o) to Method1(int o). Now, both o2.Method1()
& o1.Method1() are correct. So, it's only hiding it if the parameters are
similar (C++ would hide it based on just the name)
"Vladimir Granitsky" <vl**********@hotmail.com> wrote in message
news:##**************@TK2MSFTNGP09.phx.gbl...
Hi guys,

Please, look at the code below and try to step into it. The compiled code
calls the loosely typed method public void Method1(object o) !?!?

Am I right that C# compiler does wrong overload resolution ?

I've used parameters of type object and string here, just to illustrate the
problem. Really I have a bit more deep inheritance graph, and the things get
more interesting if the strongly typed overload is like override public void
Method1(BaseType x). When I call it with parameter of type SubType (that
inherits BaseType) the right method is called.

Thanks for any useful points.

Regadrs,
Vladimir Granitsky
using System;using System.Diagnostics;namespace OverloadResolution{
public class Class1 { virtual public void Method1(string s)
{ Trace.WriteLine("Class1.Method1"); } } public
class Class2 : Class1 { override public void Method1(string s)
{ Trace.WriteLine("Class2.Method1a"); } public void
Method1(object o) {
Trace.WriteLine("Class2.Method1b"); } } class Client
{ [STAThread] static void Main(string[] args)
{ string s = "blah"; Class2 o2 = new Class2();
o2.Method1(s); } }}
Nov 17 '05 #4
> Am I right that C# compiler does wrong overload resolution ?

The compiler matches the C# specification here, even though it is surprising
behavior.

The full story of overload resolution is incredibly complicated and it is
hard to reverse engineer the rules by looking at examples. There are two
rules that apply here.

1. Overrides are not included in the set of candidate methods during
resolution. This may seem counterintuitive since an override is more specific
and therefore a better match. However, an override is logically the same
method as the original declared in the base class. The original is still a
candidate after we remove the overrides. If the remaining steps in the
resolution process select the original method (i.e. the virtual version),
dynamic binding will ensure that the correct override version executes.

2. All candidate methods must come from the same type. We use the lowest
type in the hierarchy that contains an applicable method. We would prefer to
select a method from the lowest type since it is the most specific and we
assume it would perform the most appropriate operation. Only if the lowest
type does not contain an applicable method do we look further up the
hierarchy. We keep all the methods declared in that type and discard all
methods declared in all other types. We discard methods in the other types
even if their parameter list is a better match.
Nov 17 '05 #5
Hi James,
Thanks for the interesting response. I agree that the question is - Is this a C# compiler bug or a specification design issue. Will look forward for someone to answer. My comment are below.

"James Curran" <ja*********@mvps.org> wrote in message news:uU**************@TK2MSFTNGP14.phx.gbl...
Well, I'm gonna guess that it's working according to the C# spec, but
I'm leaning toward it being a rather bad design choice on the spec writers.

To follow what going on, add the lines:
Class1 o1 = o2;
o1.Method1(s);

to the end of your Main() function. Run it, and you'll note that while
o2.Method1() give the wrong response, o1.Method1() is correct.
Yes, I know, This is bacause if the method is declared vurtual in the base class, the latest override will be invoked, even if the object is cast to the base type. Currently I resolve this issue by calling ((Class1)o2).Method1(s);
Now, replace the "override" with "new" (or just delete it). Now,
o2.Method1() is correct, while o1.Method1() is wrong.
I think we can't say wrong here. The "new" keyword prevents Class2.Method1(string s) from being an override of Class1.Method1(string s) and the rule i mentioned above do not apply. So if you have a variable of type Class1 pointing to an instance of Class2, the method of Class1 will be invoked. I think, this is normal behaviour.

So, what I THOUGHT was happening was that Method1(object) was hiding the
Class1.Method1() (including hiding it's override). BUT, now change Method1(object o) to Method1(int o). Now, both o2.Method1()
& o1.Method1() are correct. So, it's only hiding it if the parameters are
similar (C++ would hide it based on just the name)
I think this is because string cannot cast to int and the compiler takes the right way.



"Vladimir Granitsky" <vl**********@hotmail.com> wrote in message
news:##**************@TK2MSFTNGP09.phx.gbl...
Hi guys,

Please, look at the code below and try to step into it. The compiled code
calls the loosely typed method public void Method1(object o) !?!?

Am I right that C# compiler does wrong overload resolution ?

I've used parameters of type object and string here, just to illustrate the
problem. Really I have a bit more deep inheritance graph, and the things get
more interesting if the strongly typed overload is like override public void
Method1(BaseType x). When I call it with parameter of type SubType (that
inherits BaseType) the right method is called.

Thanks for any useful points.

Regadrs,
Vladimir Granitsky
using System;using System.Diagnostics;namespace OverloadResolution{
public class Class1 { virtual public void Method1(string s)
{ Trace.WriteLine("Class1.Method1"); } } public
class Class2 : Class1 { override public void Method1(string s)
{ Trace.WriteLine("Class2.Method1a"); } public void
Method1(object o) {
Trace.WriteLine("Class2.Method1b"); } } class Client
{ [STAThread] static void Main(string[] args)
{ string s = "blah"; Class2 o2 = new Class2();
o2.Method1(s); } }}

Nov 17 '05 #6
> 2. All candidate methods must come from the same type. We use the
lowest
type in the hierarchy that contains an applicable method. We would prefer to select a method from the lowest type since it is the most specific and we assume it would perform the most appropriate operation. Only if the lowest type does not contain an applicable method do we look further up the
hierarchy. We keep all the methods declared in that type and discard all methods declared in all other types. We discard methods in the other types even if their parameter list is a better match.


I find rule #2 jarring... it runs contrary to my understanding of how
inheritance should work, let alone overloading. I had always thought
that if I were to declare:

public class Class1
{
public void Method1(string s) {...}
}

public class Class2 : Class1
{
public void Method1(object o) {..}
}

and called

Class2 c2 = new Class2();
c2.Method1("Hello world");

then of course the method invoked would be the method from the base
class, Class1. You're telling me that the Class2 method would be
invoked instead? That's loopy!

Nov 17 '05 #7
Hi everybody,
At first thank you all for the interesting and reasonable responses.

It seems that compiler beheaves regarding the specification. Yet yesterday I tried to read and understand the spec, but left it after getting headache. :) As MarkT says "The full story of overload resolution is incredibly complicated ..."

Anyway, do you think that this behaviour is right even if it is correct regarding the specification. Shoud we report this and ask for specification changes ?

In between I tryed to test the case in VB.NET, and just want to let you know that the "problem" does not apear there (You may try the code below).
A colleague of mine says that this "wrong" behaviour is missing in C++.

Cheers,
Vladimir
Class Class1
Public Overridable Sub Method1(ByVal s As String)
Trace.WriteLine("Class1.Method1")
End Sub
End Class

Class Class2
Inherits Class1

Public Overloads Overrides Sub Method1(ByVal s As String)
Trace.WriteLine("Class2.Method1a")
End Sub

Public Overloads Sub Method1(ByVal o As Object)
Trace.WriteLine("Class2.Method1b")
End Sub

End Class

Module Module1
Sub Main()
Dim o2 As Class2 = New Class2
Dim s As String = "blah"
o2.Method1(s)
End Sub
End Module

"Vladimir Granitsky" <vl**********@hotmail.com> wrote in message news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi guys,

Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!?

Am I right that C# compiler does wrong overload resolution ?

I've used parameters of type object and string here, just to illustrate the problem. Really I have a bit more deep inheritance graph, and the things get more interesting if the strongly typed overload is like override public void Method1(BaseType x). When I call it with parameter of type SubType (that inherits BaseType) the right method is called.

Thanks for any useful points.

Regadrs,
Vladimir Granitsky
using System;using System.Diagnostics;namespace OverloadResolution{ public class Class1 { virtual public void Method1(string s) { Trace.WriteLine("Class1.Method1"); } }
public class Class2 : Class1 { override public void Method1(string s) { Trace.WriteLine("Class2.Method1a"); }
public void Method1(object o) { Trace.WriteLine("Class2.Method1b"); } }
class Client { [STAThread] static void Main(string[] args) { string s = "blah"; Class2 o2 = new Class2(); o2.Method1(s); } }}

Nov 17 '05 #8
Hi Marcin ,
Thanks for the reply.

Changes in Main() cast the o2 to Class1 and it is normal to get right
result. In fact currently I resolve the problem rigth this way.

adding method into Class1, and "override" it in Class2 also is interesting.
But what if I do not need Method1(object o) in Class 1 ?

As I getting more responses I think that the behaviour is regarding the
spec, but the spec is wrongly witten here.

Regards,
Vladimir
"Marcin Grzebski" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:d3**********@nemesis.news.tpi.pl...
Hi Vladimir,

I work with .NET Framework 1.0 has the same "wrong" result!

Only change in "Main":
static void Main(string[] args)
{
string s = "blah";
Class1 o2 = new Class2();
o2.Method1(s);
}

or adding method into Class1, and "override" it in Class2

public virtual void Method1(object o) {
Console.WriteLine("Class1.Method1(object)");
}

gives me "right" result...

It looks like an overriden method has less "priority" than
a non-overriden methods in case of overload.
When i replaced method of Class2:
new public void Method1(string s)
{
Trace.WriteLine("Class2.Method1(string)");
}
then it was used by overload...

Strange and creepy :-(

Marcin
Hi guys,

Please, look at the code below and try to step into it. The compiled
code calls the loosely typed method public void Method1(object o) !?!?

Am I right that C# compiler does wrong overload resolution ?

I've used parameters of type object and string here, just to illustrate
the problem. Really I have a bit more deep inheritance graph, and the
things get more interesting if the strongly typed overload is
like override public void Method1(BaseType x). When I call it with
parameter of type SubType (that inherits BaseType) the right method is
called.

Thanks for any useful points.

Regadrs,
Vladimir Granitsky

using System;
using System.Diagnostics;
namespace OverloadResolution
{
public class Class1
{
virtual public void Method1(string s)
{
Trace.WriteLine("Class1.Method1");
}
}

public class Class2 : Class1
{
override public void Method1(string s)
{
Trace.WriteLine("Class2.Method1a");
}

public void Method1(object o)
{
Trace.WriteLine("Class2.Method1b");
}
}

class Client
{
[STAThread]
static void Main(string[] args)
{
string s = "blah";
Class2 o2 = new Class2();
o2.Method1(s);
}
}
}

Nov 17 '05 #9
No, that makes sense (It's also how it done in C++). Consider if you
had written this without Class1.Method1. Naturally, you'd expect
Class2.Method1 to be called, as it's the only Method1. Now consider if,
years later, some maintence programmer added Method1 to Class1. Next time
you recompile your code, suddenly a different function is being called.
That's not the way it should work. So, method1 in a derived class hide base
methods with the same name.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I find rule #2 jarring... it runs contrary to my understanding of how
inheritance should work, let alone overloading. I had always thought
that if I were to declare:

public class Class1
{
public void Method1(string s) {...}
}

public class Class2 : Class1
{
public void Method1(object o) {..}
}

and called

Class2 c2 = new Class2();
c2.Method1("Hello world");

then of course the method invoked would be the method from the base
class, Class1. You're telling me that the Class2 method would be
invoked instead? That's loopy!

Nov 17 '05 #10
That makes more sense, now. I'm still not 100% sold, but I know that
the .NET Framework designers spent a lot of time thinking about
versioning, the contracts implied by class interfaces, and not breaking
derived classes / clients just because a base class changed. That's one
of the reasons why checked exceptions weren't included in C#. So, I can
see them doing this as well in order to solve similar versioning
problems.

It still screws up my notions of class inheritance, though.

Nov 17 '05 #11
I agree. But the case Bruce introduces is different.

"James Curran" <ja*********@mvps.org> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
No, that makes sense (It's also how it done in C++). Consider if you
had written this without Class1.Method1. Naturally, you'd expect
Class2.Method1 to be called, as it's the only Method1. Now consider if,
years later, some maintence programmer added Method1 to Class1. Next time
you recompile your code, suddenly a different function is being called.
That's not the way it should work. So, method1 in a derived class hide base methods with the same name.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I find rule #2 jarring... it runs contrary to my understanding of how
inheritance should work, let alone overloading. I had always thought
that if I were to declare:

public class Class1
{
public void Method1(string s) {...}
}

public class Class2 : Class1
{
public void Method1(object o) {..}
}

and called

Class2 c2 = new Class2();
c2.Method1("Hello world");

then of course the method invoked would be the method from the base
class, Class1. You're telling me that the Class2 method would be
invoked instead? That's loopy!


Nov 17 '05 #12
Hi,
I agree that candidates must come from the same class. Otherwise we'll fall into the problem described by James.

But I cannot agree that the overrides should be excluded from the candidate list. Also the argument that they are logically same as the original in the base class I think is weak. When a derived class overrides the method in a base class, it states that he know how to perform given operation (even better, with or without the help of the base class). That is the override method should be considered as it comes from the derived class. We cannot find any solid argument that they are not from it.
"Employee methods are preferred over Person methods".

MarkT, I understant what do you mean here, but I think, that through overeload resolution process Employee methods shoud include the overrides too. Otherwise, as it is in fact, the overload resolution cuts off the chance dynamic binding to do it's job.

I have coped with the task to prepare a managed C++ example (included below). The "problem" is missing there. It's looks like only C# is so "smart" in overload resolution. I really like C# and I've never preffered any other language. But this is very concerning.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Diagnostics;

__gc public class CClass1
{
public:
virtual void Method1(String *s)
{
System::Diagnostics::Trace::WriteLine("Class1.Meth od1");
}
};

__gc public class CClass2 : public CClass1
{
public:
void Method1(String *s)
{
System::Diagnostics::Trace::WriteLine("Class2.Meth od1a");
}

void Method1(Object *o)
{
System::Diagnostics::Trace::WriteLine("Class2.Meth od1b");
}
};

int _tmain()
{
CClass2* o2 = new CClass2();
o2->Method1("Hello World!");
return 0;
}

"MarkT [developmentor]" <Ma****************@discussions.microsoft.com> wrote in message news:22**********************************@microsof t.com...
You're telling me that the Class2 method would be
invoked instead? That's loopy!


I had to adjust my own thinking a bit when I researched these rules. They do
make sense, they just don't match everyone's initial intuition.

It helped me to replace Class1 and Class2 with Person and Employee. If we
have an Employee object it sounds reasonable to say "Employee methods are
preferred over Person methods".

Mark

Nov 17 '05 #13
I've observed the behaviour you second point refers to, but can't find it in
ECMA334. Do you by any chance know which section it is in?

Thanks,
Aaron.

"MarkT [developmentor]" wrote:
2. All candidate methods must come from the same type. We use the lowest
type in the hierarchy that contains an applicable method. We would prefer to
select a method from the lowest type since it is the most specific and we
assume it would perform the most appropriate operation. Only if the lowest
type does not contain an applicable method do we look further up the
hierarchy. We keep all the methods declared in that type and discard all
methods declared in all other types. We discard methods in the other types
even if their parameter list is a better match.

Nov 17 '05 #14

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

Similar topics

7
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
0
by: Eph0nk | last post by:
Hi, I get an overload resolution failed error (Overload resolution failed because no accessible 'New' can be called without a narrowing conversion), and I can't seem to find a lot of relevant...
2
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
3
by: Christof Nordiek | last post by:
Given the following code Is there any way to call one of the Bar methods. using System; class Program { static void Main() { Foo<string, stringfoo = new Foo<string,string>();
14
by: =?Utf-8?B?R3JlZ2cgV2Fsa2Vy?= | last post by:
Hopefully someone can set me straight on this issue. I was testing some code where I have a method that is being overriden. The method overrides were working fine until I passed a literal 0 (int...
2
by: xtrigger303 | last post by:
Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {};
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
1
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
2
by: zbigniew | last post by:
Can someone explain me how overload resolution works in C++? For example if I have function void f(char, int); and I will call f('A', 3.1) or f(1.5, 3.1F) what would be the result? Thanks
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.