I am learning about virtual functions in C# and I've come across this problem I can't wrap my head around. I have the following code in C#:
namespace ConsoleApp1
{
class A
{
public virtual void F()
{
Console.WriteLine("A.F");
}
}
class B : A
{
public override void F()
{
Console.WriteLine("B.F");
}
}
class C : B
{
public new virtual void F()
{
Console.WriteLine("C.F");
}
}
class D : C
{
public override void F()
{
Console.WriteLine("D.F");
}
}
class Program
{
static void Main(string[] args)
{
D d = new D();
A a = d;
B b = d;
C c = d;
a.F();
b.F();
c.F();
d.F();
c = new C();
c.F();
Console.ReadLine();
}
}
}
Why the output is this? It doesn't make sense to me at all. Can someone explain? Output:
B.F
B.F
D.F
D.F
C.F