I have this code:
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Foo foo = new Foo();
Bar bar = new Bar();
Baz baz = new Baz();
Foo fooBar = new Bar();
Foo fooBaz = new Baz();
Bar barBaz = new Baz();
foo.Test();
bar.Test();
baz.Test();
fooBar.Test();
fooBaz.Test();
barBaz.Test();
Console.ReadLine();
}
}
internal class Foo
{
public virtual void Test()
{
Console.WriteLine("Foo");
}
}
internal class Bar : Foo
{
public new virtual void Test()
{
Console.WriteLine("Bar");
}
}
internal class Baz : Bar
{
public override void Test()
{
Console.WriteLine("Baz");
}
}
}
it outputs me:
Foo
Bar
Baz
Foo
Foo
Baz
But, I thought it should be:
Foo
Bar
Baz
Foo
Baz
Baz
Because Baz is overriding the method. What is happening here? Am I missing something? Why did the output for fooBaz.Test() is "Foo" instead of "Baz"?