I\'m getting a warning from ReSharper about a call to a virtual member from my objects constructor.
Why would this be something not to do?
One important aspect of this question which other answers have not yet addressed is that it is safe for a base-class to call virtual members from within its constructor if that is what the derived classes are expecting it to do. In such cases, the designer of the derived class is responsible for ensuring that any methods which are run before construction is complete will behave as sensibly as they can under the circumstances. For example, in C++/CLI, constructors are wrapped in code which will call Dispose
on the partially-constructed object if construction fails. Calling Dispose
in such cases is often necessary to prevent resource leaks, but Dispose
methods must be prepared for the possibility that the object upon which they are run may not have been fully constructed.