Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derived method stronger than override in c#?

(again annoying question...)

after asking this before - ( which is partly related to my question) - i got an answer :

See §7.6.5.1 of the C# 4 spec:

The set of candidate methods is reduced to contain only methods from the most derived types: For each method C.F in the set, where C is the type in which the method F is declared, all methods declared in a base type of C are removed from the set.

ok.

i have this code : //.Dump() is like a WriteLine command...

 public class Base
    {
        public void Foo(string strings)  { "1".Dump();}
        public virtual void  Foo(object strings)  { "2".Dump();}
    }

    public class Child : Base
    {

        public override void  Foo(object strings)  { "4".Dump();}
    }

but this code :

Child c = new Child();
c.Foo("d");

emits : "1"

but wait ...

didnt we say that is reduced to contain only methods from the most derived types: ?

Child has 1 function from its father public void Foo(string strings) and a NEARER override function.

so why did he choose its base's function ? does the inherited function is CLOSER than the override ?

is it related to the fact that override is at runtime ?

help please.

edit

what about this situation ?

public class Base
    {    
        public virtual void  Foo(int x)  { "1".Dump();}
    }

    public class Child : Base
    {
        public override void  Foo(int x)  { "2".Dump();}
        public void Foo(object x) { "3".Dump();}    
    }

void Main()
{
    Child c = new Child();
    c.Foo(6); //emits "3"
}
like image 611
Royi Namir Avatar asked Dec 21 '22 21:12

Royi Namir


2 Answers

That's because your child takes an object.

public class Child : Base
{
    public override void Foo(object strings)  { "4".Dump();}
}

Make it string and then child one will be called.

public class Child : Base
{
    public override void Foo(string strings)  { "4".Dump();}
}

Why this?

Because compiler sees that child has object prameter it has to convert to string while in base class it is readily available as string.

So it calls base one.

Though Overridden function is nearer in the child class. But here rules are different in child and base. Child has object and base has string. It was fair if both had object or both had string.

I read this in Jon Skeet's C# in Depth Overloading Section

like image 142
Nikhil Agrawal Avatar answered Dec 23 '22 10:12

Nikhil Agrawal


There are two concepts here which are being confused, overloading and overriding. Overloading is the concept of having multiple signatures for a function of the same name and choosing one to call based on that signature. Overriding is the concept of re-defining a method in a derived class.

Whether or not the child class overrode one of the method definitions does not matter at all to which function will be called because it does not change the signature of either method. By definition and construction overriding a method cannot change it's signature.

So, if the signature is unchanged exactly the same mechanics for determining the correct function to call based on the signature will be used against both the parent and child classes.

Update

In fact there is a little bit more to it as Eric Lippert points out in his blog. It turns out in fact that if there is a method that matches the overload in the child class it will not look for any methods in base classes. The reasons are sane - avoiding breaking changes - but the result is somewhat illogical when you own both the base and child class.

I can only echo Jon Skeet: "Given this oddness, my advice would be to avoid overloading across inheritance boundaries... at least with methods where more than one method could be applicable for a given call if you flattened the hierarchy"

like image 32
Slugart Avatar answered Dec 23 '22 11:12

Slugart