public class A
{
public A() { }
public void Do() { Console.Write("A"); }
}
public class B : A
{
public B() { }
public void Do() { Console.Write("B"); }
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.Do(); //<-- outputs B
(b as A).Do(); //<-- outputs A
}
}
compiler warns for hiding not overriding:
Warning 1 'ConsoleApplication5.B.Do()' hides inherited member
'ConsoleApplication5.A.Do()'. Use the new keyword if hiding was
intended. c:\Program.cs 18 21 ConsoleApplication5
that is since you are not overriding anything, but simply hiding the method from A.
however
public class A
{
public A() { }
public virtual void Do() { Console.Write("A"); }
}
public class B : A
{
public B() { }
public override void Do() { Console.Write("B"); }
}
calls B twice, when method is overridden.