Consider the following few classes and interfaces :
class Program
{
interface I { void foo(); }
abstract class A : I
{
public virtual void foo() { Console.WriteLine("A"); }
}
class B : A
{
public new virtual void foo() { Console.WriteLine("B"); }
}
class C : B
{
public override void foo() { Console.WriteLine("C"); }
}
static void Main(string[] args)
{
var c = new C();
(c as I).foo();
(c as A).foo();
(c as B).foo();
}
}
It outputs :
A A C
where I would (naively) expect "C C C"
if indeed I replace "new virtual" by "override" in class B, that is what I get.
In the MSIL, I get
.method public hidebysig newslot virtual instance void foo () cil managed
instead of
method public hidebysig virtual instance void foo () cil managed
What does "new virtual" on a method?
Ho can you explain the behavior of my program?