Quiz: Virtual Methods - Answer!

Well, glad to see many of you got it right, I think FxCop is doing its job! Google helped me find a couple of posts on this already out there:
https://weblogs.asp.net/trichards/archive/2003/06/20/9026.aspx

https://dotnetjunkies.com/WebLog/mlevison/archive/2004/04/22/11976.aspx

Here is the full text I will add to the API Design Guidelines document. Let me know what you think.

Consider the example below which prints out “value != 42, what is wrong” when a new instance of Derived is created. The implementer of the derived class assumes that the value will be set to 42 before anyone can call Method1(). But that is not true because the base class’s constructor is called before the Derived class’s constructor finishes, so any calls it makes to Method1() causes the problem.

public class Base

{

    public Base()

  {

        Method1();

    }

    public virtual void Method1() {

        Console.WriteLine("In Base's Method1()");

    }

}

public class Derived: Base

{

    private int value;

    public Derived()

    {

        value = 42;

    }

    public override void Method1()

    {

        if (value == 42) Console.WriteLine("value == 42, all is good");

        else Console.WriteLine("value != 42, what is wrong?");

    }

}