Overload resolution and virtual methods

前端 未结 5 637
臣服心动
臣服心动 2020-11-28 11:15

Consider the following code (it\'s a little long, but hopefully you can follow):

class A
{
}

class B : A
{
}

class C
{
    public virtual void Foo(B b)
            


        
5条回答
  •  春和景丽
    2020-11-28 11:45

    I think that when implementing another class it looks as far up the tree to get an solid implementation of a method. As there is no method being called it is using the base class.

    public void Foo(A a){
        Console.WriteLine("Foo(A)" + a.GetType().Name);
        Console.WriteLine("Foo(A)" +a.GetType().BaseType );
    }
    

    thats a guess i am no pro at .Net

提交回复
热议问题