Can someone explain to me why is the overridden method being called when I cast the class into the base one:
class Base
{
public virtual void VirtualMethod()
{
Console.WriteLine("Base virtual method");
}
}
sealed class Derived : Base
{
public override void VirtualMethod()
{
Console.WriteLine("Overriden method");
}
}
static void Main(String[] args)
{
Derived d = new Derived();
((Base)d).VirtualMethod();
}
I mean this code prints:
Overriden method
and not
Base virtual method
Its a run-time or compile-time future?
I know i can call the Base's virtual method from the derived by calling base.VirtualMethod()
but can I call it from outside? (like from Main
or some other class)