473,545 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
+ 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(BaseTyp e 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.Diagnost ics;namespace OverloadResolut ion{ public class Class1 { virtual public void Method1(string s) { Trace.WriteLine ("Class1.Method 1"); } } public class Class2 : Class1 { override public void Method1(string s) { Trace.WriteLine ("Class2.Method 1a"); } public void Method1(object o) { Trace.WriteLine ("Class2.Method 1b"); } } class Client { [STAThread] static void Main(string[] args) { string s = "blah"; Class2 o2 = new Class2(); o2.Method1(s); } }}
Nov 17 '05 #1
13 2704
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.WriteLi ne("Class1.Meth od1(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.Method 1(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(BaseTyp e 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.Diagnost ics;
namespace OverloadResolut ion
{
public class Class1
{
virtual public void Method1(string s)
{
Trace.WriteLine ("Class1.Method 1");
}
}

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

public void Method1(object o)
{
Trace.WriteLine ("Class2.Method 1b");
}
}

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**********@h otmail.com> wrote in message
news:##******** ******@TK2MSFTN GP09.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(BaseTyp e 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.Diagnost ics;namespace OverloadResolut ion{
public class Class1 { virtual public void Method1(string s)
{ Trace.WriteLine ("Class1.Method 1"); } } public
class Class2 : Class1 { override public void Method1(string s)
{ Trace.WriteLine ("Class2.Method 1a"); } public void
Method1(object o) {
Trace.WriteLine ("Class2.Method 1b"); } } 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 counterintuitiv e 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*********@mv ps.org> wrote in message news:uU******** ******@TK2MSFTN GP14.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).Me thod1(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**********@h otmail.com> wrote in message
news:##******** ******@TK2MSFTN GP09.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(BaseTyp e 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.Diagnost ics;namespace OverloadResolut ion{
public class Class1 { virtual public void Method1(string s)
{ Trace.WriteLine ("Class1.Method 1"); } } public
class Class2 : Class1 { override public void Method1(string s)
{ Trace.WriteLine ("Class2.Method 1a"); } public void
Method1(object o) {
Trace.WriteLine ("Class2.Method 1b"); } } 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("Hel lo 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.Method 1")
End Sub
End Class

Class Class2
Inherits Class1

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

Public Overloads Sub Method1(ByVal o As Object)
Trace.WriteLine ("Class2.Method 1b")
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**********@h otmail.com> wrote in message news:%2******** **********@TK2M SFTNGP09.phx.gb l...
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(BaseTyp e 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.Diagnost ics;namespace OverloadResolut ion{ public class Class1 { virtual public void Method1(string s) { Trace.WriteLine ("Class1.Method 1"); } }
public class Class2 : Class1 { override public void Method1(string s) { Trace.WriteLine ("Class2.Method 1a"); }
public void Method1(object o) { Trace.WriteLine ("Class2.Method 1b"); } }
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*******@taxu ssi.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.WriteLi ne("Class1.Meth od1(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.Method 1(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(BaseTyp e 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.Diagnost ics;
namespace OverloadResolut ion
{
public class Class1
{
virtual public void Method1(string s)
{
Trace.WriteLine ("Class1.Method 1");
}
}

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

public void Method1(object o)
{
Trace.WriteLine ("Class2.Method 1b");
}
}

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*******@cana da.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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("Hel lo 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

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

Similar topics

7
2080
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
0
2581
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 information by googling the web. Function GetFromXml(Id As String, Node As String) Dim NodeValue As String Dim xpathDoc As XPathDocument Dim...
2
1837
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
1517
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
1592
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 value) in as the lone argument. Well there are two possible overrides that could be called with a single argument. The first one takes an...
2
1856
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
3677
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 explicitly takes a pointer-to-member with no parameters, why is the lookup for the overloaded function not able to use the number of arguments to...
1
1728
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 = X(1,2), the elements 1 and 2 of X gets assigned to Y. In this case, the operator () overload should create a copy that is unmodifiable. In the 2nd...
2
2690
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
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5349
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.