C# Virtual, Override Execution Path

class zzz
{
    public static void Main()
    {
        yyy a = new vvv();
        xxx b = new vvv();
        www c = new vvv();
        vvv d = new vvv();

        a.pqr();
        b.pqr();
        c.pqr();
        d.pqr();
    }
}
class yyy
{
    public virtual void pqr()
    {
        System.Console.WriteLine("yyy pqr");
    }
}
class xxx : yyy
{
    public override void pqr()
    {
        System.Console.WriteLine("xxx pqr");
    }
}
class www : xxx
{
    public override void pqr()
    {
        System.Console.WriteLine("www pqr");
    }
}
class vvv : www
{
    public override void pqr()
    {
        System.Console.WriteLine("vvv pqr");
    }
}

Output:

 vvv pqr
 vvv pqr
 vvv pqr
 vvv pqr

I know the question i am going to ask will earn me some down votes. But i don't have any other choice. Can someone help me to understand execution path of this code? What i was thinking that a.pqr() will stop at "xxx pqr", But i was wrong. And wrong for others too.

I failed to elaborate my problem so i am trying it again.

  1. if i take a.pqr() into consideration:

    Execution will start from yyy [What i think, need to be corrected.] yyy.pqr() is virtual now it will look at xxx.pqr() which is override. Shouldn't it stop here? or it will traverse to www.pqr() because xxx.pqr() is carrying virtual properties from yyy(base class) and it will end up at vvv.pqr() [Actual type] ??

  2. For c.pqr() :

    can i get help to understand why does it call "vvv pqr"? Its override. Still there is connection with its base? i looked at it its override so i thought answer would be "www pqr" because its not virtual it won't traverse,But it did. If i make www.pqr() new, output will be "www pqr" because its new and i don't have to look down. I am in dilemma with basic understanding.

Jon Skeet
people
quotationmark

The method is declared virtual in yyy, and overridden in every descendant class. It's one method with lots of implementations, as far as the CLR is concerned. The compiler emits a call to that method, and at execution time the CLR will consider the actual type of the object that the method is called on, and execute the implementation which overrides it closest to that type.

From the C# 5 specification, section 10.6.3:

In a virtual method invocation, the run-time type of the instance for which that invocation takes place determines the actual method implementation to invoke.

...

For every virtual method declared in or inherited by a class, there exists a most derived implementation of the method with respect to that class. The most derived implementation of a virtual method M with respect to a class R is determined as follows:

  • If R contains the introducing virtual declaration of M, then this is the most derived implementation of M.
  • Otherwise, if R contains an override of M, then this is the most derived implementation of M.
  • Otherwise, the most derived implementation of M with respect to R is the same as the most derived implementation of M with respect to the direct base class of R.

people

See more on this question at Stackoverflow