0

I would like to ask you for an explanation of this example:

public class A : B
{
    public override void Method()
    {
        Console.WriteLine("A");
    }
}

public class B
{
    public virtual void Method()
    {
        Console.WriteLine("B");
    }
}

Equation:

void Main()
{
    B b = (B)new A();

    b.Method(); // result A
}

Why upcasting did not work in this case?

1 Answers1

1

How microsoft docs say:

If you want your derived class to have a member with the same name as a member in a base class, you can use the new keyword to hide the base class member. The new keyword is put before the return type of a class member that is being replaced. The following code provides an example:

Den
  • 650
  • 6
  • 15