Aqra bl-Ingliż Editja

Ixxerja permezz ta’


CA2214: Do not call overridable methods in constructors

Property Value
Rule ID CA2214
Title Do not call overridable methods in constructors
Category Usage
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 9 No

Cause

The constructor of an unsealed type calls a virtual method defined in its class.

Rule description

When a virtual method is called, the actual type that executes the method is not selected until run time. When a constructor calls a virtual method, it's possible that the constructor for the instance that invokes the method has not executed. This could lead to errors or unexpected behavior, if an overridden virtual method relies on initialization and other configuration in the constructor.

How to fix violations

To fix a violation of this rule, do not call a type's virtual methods from within the type's constructors.

When to suppress warnings

Do not suppress a warning from this rule. The constructor should be redesigned to eliminate the call to the virtual method.

Example

The following example demonstrates the effect of violating this rule. The test application creates an instance of DerivedType, which causes its base class (BadlyConstructedType) constructor to execute. BadlyConstructedType's constructor incorrectly calls the virtual method DoSomething. As the output shows, DerivedType.DoSomething() executes before DerivedType's constructor executes.

C#
public class BadlyConstructedType
{
    protected string initialized = "No";

    public BadlyConstructedType()
    {
        Console.WriteLine("Calling base ctor.");
        // Violates rule: DoNotCallOverridableMethodsInConstructors.
        DoSomething();
    }
    // This will be overridden in the derived type.
    public virtual void DoSomething()
    {
        Console.WriteLine("Base DoSomething");
    }
}

public class DerivedType : BadlyConstructedType
{
    public DerivedType()
    {
        Console.WriteLine("Calling derived ctor.");
        initialized = "Yes";
    }
    public override void DoSomething()
    {
        Console.WriteLine("Derived DoSomething is called - initialized ? {0}", initialized);
    }
}

public class TestBadlyConstructedType
{
    public static void Main2214()
    {
        DerivedType derivedInstance = new DerivedType();
    }
}

This example produces the following output:

txt
Calling base ctor.
Derived DoSomething is called - initialized ? No
Calling derived ctor.