K
Karahan Celikel
Here are three simple classes:
class A
{
public void DoIt(B b)
{
DoSomething(b);
}
public void DoSomething(B b)
{
}
public void DoSomething(C c)
{
}
}
class B
{
}
class C : B
{
}
Here is some simple code with line numbers:
1. A a = new A();
2. C c = new C();
3. a.DoSomething(c);
4. a.DoIt(c);
Here is my question:
Line 3 will cause the overloaded method A.DoSomething(C c) to be called, but
line 4 causes A.DoSomething(B b) to be called. Why? That seems pretty wack
if you ask me. Yes, I can see that the first method to be called is DoIt(B
b), which then apparently causes the "wrong" subsequent method to be called.
But what is the underlying rationale for such behavior?
class A
{
public void DoIt(B b)
{
DoSomething(b);
}
public void DoSomething(B b)
{
}
public void DoSomething(C c)
{
}
}
class B
{
}
class C : B
{
}
Here is some simple code with line numbers:
1. A a = new A();
2. C c = new C();
3. a.DoSomething(c);
4. a.DoIt(c);
Here is my question:
Line 3 will cause the overloaded method A.DoSomething(C c) to be called, but
line 4 causes A.DoSomething(B b) to be called. Why? That seems pretty wack
if you ask me. Yes, I can see that the first method to be called is DoIt(B
b), which then apparently causes the "wrong" subsequent method to be called.
But what is the underlying rationale for such behavior?