I always thought base.Something
was equivalent to ((Parent)this).Something
, but apparently that's not the case. I thought that overriding methods eliminated the possibility of the original virtual method being called.
Why is the third output different?
void Main() {
Child child = new Child();
child.Method(); //output "Child here!"
((Parent)child).Method(); //output "Child here!"
child.BaseMethod(); //output "Parent here!"
}
class Parent {
public virtual void Method() {
Console.WriteLine("Parent here!");
}
}
class Child : Parent {
public override void Method() {
Console.WriteLine ("Child here!");
}
public void BaseMethod() {
base.Method();
}
}