I have two Data classes: BaseDataClass and DerivedDataClass which derives from first one.
I also have two Consuming classes: ConsumingBaseClass and ConsumingDerivedClass which derives from first one.
In ConsumingBaseClass I have virtual method DoWork, which accepts DerivedDataClass and do some work.
In ConsumingDerivedClass I have override for method DoWork and also I have overload for DoWork which accepts BaseDataClass.
When I try to call DoWork, passing instance of DerivedDataClass, DoWork(BaseDataClass) is called instead of DoWork(DerivedDataClass).
Have anybody idea, why wrong method is called?
Following code demostrates the problem:
class Program
{
private static void Main(string[] args)
{
ConsumingDerivedClass x = new ConsumingDerivedClass();
// Wrong DoWork is called - expected calling of DoWork(DerivedDataClass) but actually called DoWork(BaseDataClass)
x.DoWork(new DerivedDataClass());
Console.ReadKey();
}
}
public class ConsumingBaseClass
{
public virtual void DoWork(DerivedDataClass instance)
{
Console.WriteLine("ConsumingBaseClass.DoWork(DerivedDataClass); Type of argument is '{0}'", instance.GetType());
}
}
public class ConsumingDerivedClass : ConsumingBaseClass
{
public override void DoWork(DerivedDataClass instance)
{
Console.WriteLine("ConsumingDerivedClass.DoWork(DerivedDataClass); Type of argument is '{0}'", instance.GetType());
base.DoWork(instance);
// Some additional logic
}
public void DoWork(BaseDataClass instance)
{
Console.WriteLine("ConsumingDerivedClass.DoWork(BaseDataClass); Type of argument is '{0}'", instance.GetType());
DerivedDataClass derivedInstance = new DerivedDataClass();
// Some logic based on what is in baseInstacne
derivedInstance.SomeProperty = "Value, got from some logic";
base.DoWork(derivedInstance);
// Some additional logic
}
}
public class BaseDataClass
{ }
public class DerivedDataClass : BaseDataClass
{
public string SomeProperty { get; set; }
}
The right method is being called according to the language specification. It’s your expectations which are wrong.
That’s the problem, basically. Overload resolution doesn’t work the way you think it does. The compiler works from the compile-time type of the target of the call, up through base classes towards
object. Only methods newly declared (not even overridden) are considered.You can change which overload would be used by writing:
Ideally though, if you’ve got something this brittle, it would be worth giving the methods different names. Overloads should have the same effect, just providing information in different ways.
Basically, have a look at the first question on my brainteasers page, and the details of the first answer. I also have an article on overload resolution which you may find useful.