Consider this code:
public class A
{
public void Method1()
{
Console.WriteLine("A.Method1");
}
public virtual void Method2()
{
Console.WriteLine("A.Method2");
}
}
public class B : A
{
public new void Method1()
{
Console.WriteLine("b.Method1");
}
public override void Method2()
{
Console.WriteLine("b.Method2");
}
}
and this:
B b = new B();
b.Method1();
b.Method2();
Console.WriteLine("*********************");
A a = b;
a.Method1();
a.Method2();
This is my Result:
b.Method1
b.Method2
A.Method1
b.Method2
My question is why when we call a.Method1() I get A.Method1 instead of getting b.Method1.And why method hiding not work.
Note This line:a = b