Why is this an issue?
Calling an overridable method from a constructor could result in failures or strange behaviors when instantiating a subclass which overrides the method.
When constructing an object of a derived class, the constructor of the parent class is invoked first, and only then the constructor of the derived class is called. This sequential construction process applies to multiple levels of inheritance as well, starting from the base class and progressing to the most derived class.
If an overridable method is called within the constructor of the parent class, it may inadvertently invoke an overridden implementation in the derived class. This can lead to unexpected failures or strange behaviors because the object’s construction is still in progress and may not have reached a fully initialized state. Consequently, the overridden method may rely on uninitialized members or have assumptions about the object’s state that are not yet valid.
For example:
public class Parent
{
public Parent()
{
DoSomething(); // Noncompliant
}
public virtual void DoSomething() // can be overridden
{
// ...
}
}
public class Child : Parent
{
private string foo;
public Child(string foo) // leads to call DoSomething() in Parent constructor which triggers a NullReferenceException as foo has not yet been initialized
{
this.foo = foo;
}
public override void DoSomething()
{
Console.WriteLine(this.foo.Length);
}
}
- The
Childclass constructor starts by calling theParentclass constructor. - The
Parentclass constructor calls the methodDoSomething, which has been overridden in theChildclass. - If the behavior of the
Childclass overriddenDoSomethingmethod depends on fields that are initialized in theChildclass constructor, unexpected behavior (such as aNullReferenceException) can result, because the fields aren’t initialized yet.
How to fix it
Depending on the context, you can either:
- avoid calling overridable methods from constructors. This is the recommended approach
- ensure that the method is not overridden in any derived classes. This can be done by marking the method as
sealedin the current class - ensure that the class is not inherited from. This can be done by marking the class as
sealed
Code examples
Noncompliant code example
class Parent
{
public virtual void DoSomething() { }
}
class Child : Parent
{
public Child()
{
DoSomething(); // Noncompliant
}
}
class Parent
{
public virtual void DoSomething() { }
}
class Child : Parent
{
public Child()
{
DoSomething(); // Noncompliant
}
}
class Parent
{
public virtual void DoSomething() { }
}
class Child : Parent
{
public Child()
{
DoSomething(); // Noncompliant
}
}
Compliant solution
class Parent
{
public virtual void DoSomething() { }
}
class Child : Parent
{
public Child()
{
// Call removed
}
}
class Parent
{
public virtual void DoSomething() { }
}
class Child : Parent
{
public Child()
{
DoSomething();
}
// Method sealed to prevent overriding
public sealed override void DoSomething()
{
base.DoSomething();
}
}
class Parent
{
public virtual void DoSomething() { }
}
// Class sealed to prevent inheritance
sealed class Child : Parent
{
public Child()
{
DoSomething();
}
}
Resources
Documentation
- Microsoft Learn - Constructors
- Microsoft Learn - Inheritance
- Microsoft Learn - Polimorphism
- Microsoft Learn - Methods - Method signatures
- Stack Overflow - Answer by Eric Lippert for Overriding and calling same method in Base class constructor in C#
- Fabulous adventures in coding - Why Do Initializers Run In The Opposite Order As Constructors?