I am obtaining a warning from ReSharper in regards to a call to some virtual member from my objects constructor. Why would this be something to avoid?

(Presuming you are writing in C# here)

When an item designed in C# is built, ultimately the initializers run so as in the most derived class towards the base class, after which constructors run so as in the base class towards the most derived class (see Eric Lippert's blog for particulars why this really is).

Also insInternet objects don't change type because they are built, but begin because the most derived type, using the method table being which are more derived type. Which means that virtual method calls always operate on probably the most derived type.

Whenever you mix both of these details you're playing the issue that if one makes an online method get in touch with a constructor, which is not probably the most derived key in its inheritance hierarchy, that it'll be known as on the class whose constructor is not run, and for that reason might not be inside a appropriate condition to possess that method known as.

This issue is, obviously, mitigated should you mark your class as sealed to make sure that it's the most derived enter in the inheritance hierarchy - by which situation it's perfectly safe to call the virtual method.

The guidelines of C# are quite different from those of Java and C++.

When you're within the constructor for many object in C#, that object is available inside a fully initialized (simply not "built") form, since it's fully derived type.

namespace Demo

,", this.GetType())

      

    

    class B : A

    

    // . . .

    B b = new B() // Output: "This can be a Demo.B"



Which means that should you call an online function in the constructor of the, it'll resolve to the override in B, if your are provided.

Even when you deliberately setup A and B such as this, fully comprehending the behavior from the system, you may be set for a surprise later. Say you known as virtual functions in B's constructor, "knowing" they'd be handled by B or perhaps a as appropriate. Then the year progresses, and another person decides they have to define C, and override a few of the virtual functions there. Out of the blue B's constructor eventually ends up calling code in C, which can lead to quite surprising behavior.

It's most likely smart to avoid virtual functions in constructors anyway, because the rules are so different between C#, C++, and Java. Your developers might not know what to anticipate!

To be able to answer your question, think about this question: what's going to the below code print once the Child object is instantiated?

class Parent



    protected virtual void DoSomething() 



class Child : Parent



    protected override void DoSomething()

    



The reply is that actually a NullReferenceException is going to be tossed, because foo is null. An object's base constructor is known as before its very own constructor. By getting an online get in touch with an object's constructor you're presenting the chance that getting objects will execute code before they've been fully initialized.

Reasons from the warning happen to be referred to, but how does one fix the warning? You need to seal either class or virtual member.

  class B

  

  

  class A : B

  

  

You are able to seal class A:

  sealed class A : B

  

  

Or seal method Foo:

  class A : B

  

    protected sealed override void Foo()