Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Usage Warnings
 Do not call overridable methods in ...
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
Do not call overridable methods in constructors

TypeName

DoNotCallOverridableMethodsInConstructors

CheckId

CA2214

Category

Microsoft.Usage

Breaking Change

NonBreaking

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

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 is possible that the constructor for the instance that invokes the method has not executed.

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

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

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, and does so before DerivedType's constructor executes.

Visual Basic
Imports System

Namespace UsageLibrary

Public Class BadlyConstructedType
    Protected initialized As String = "No"
    
    
    Public Sub New()
        Console.WriteLine("Calling base ctor.")
        ' Violates rule: DoNotCallOverridableMethodsInConstructors.
        DoSomething()
    End Sub 'New
    
    ' This will be overridden in the derived type.
    Public Overridable Sub DoSomething()
        Console.WriteLine("Base DoSomething")
    End Sub 'DoSomething
End Class 'BadlyConstructedType


Public Class DerivedType
    Inherits BadlyConstructedType
    
    Public Sub New()
        Console.WriteLine("Calling derived ctor.")
        initialized = "Yes"
    End Sub 'New
    
    Public Overrides Sub DoSomething()
        Console.WriteLine("Derived DoSomething is called - initialized ? {0}", initialized)
    End Sub 'DoSomething
End Class 'DerivedType


Public Class TestBadlyConstructedType
    
    Public Shared Sub Main()
        Dim derivedInstance As New DerivedType()
    End Sub 'Main
End Class 
End Namespace
C#
using System;

namespace UsageLibrary
{
    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 Main()
        {
            DerivedType derivedInstance = new DerivedType();
        }
    }
}

This example produces the following output.

Output

Calling base ctor.
Derived DoSomething is called - initialized ? No
Calling derived ctor.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
How to Fix Violations      Jonathan Allen ... Luke Ogg   |   Edit   |  

Marking the class sealed/NotInheritable is the easiest way to correct this violation.

Note: This approach has the side-effect that you will not be able to use any virtual methods in your class.

Lazy Initialization

An alternative is to only call the virtual method on-demand. This can be accomplished by providing the resources that are dependant on the virtual method to a class and/or its derived types via public or protected properties on the base class. The properties can check whether the resources have been initialized. If they haven't, the property can call the virtual method once, caching the result for subsequent access.

Example

Lazy initialization is commonly found in abstract classes where the need exists to give derived types a chance to strong-type a property defined on the base class. Along with generics in the 2.0 framework, a solution can be devised that is type-safe and doesn't require a virtual method call in the constructor:

   class abstract Car<TEngine>
{
private TEngine engine;

public TEngine Engine
{
get
{
// create the engine if it's a null reference
return engine ?? engine = CreateEngine();
}
}

protected abstract TEngine CreateEngine();
}

class Lemon : Car<BrokenEngine>
{
protected override BrokenEngine CreateEngine()
{
return new BrokenEngine();
}
}

In the example above, the engine field is initialized by the return value of the virtual CreateEngine method, which is only called the first time the Engine property is accessed (unless the CreateEngine method returns null , in which case it will be called again each time the Engine property is accessed until a non-null value is returned).

Tags What's this?: Add a tag
Flag as ContentBug
Can also use dependency injection to circumvent.      spasecookee   |   Edit   |  
Using dependency injection in the base type will circumvent this problem, although it doesn't suppress the error. To suppress the error, one would have to mark the derived class as sealed and move the call to DoSomething() in the derived constructor.

Here is an example:

namespace UsageLibrary {
public class BadlyConstructedType {
protected string initialized = "No";

public BadlyConstructedType(string initialized) {
this.initialized = initialized;
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() : this("Yes") { }
public DerivedType(string initialized)
: base(initialized) {
Console.WriteLine("Calling derived ctor.");
}
public override void DoSomething() {
Console.WriteLine("Derived DoSomething is called - initialized ? {0}", initialized);
}
}
}

//Calling base ctor.
//Derived DoSomething is called - initialized ? Yes
//Calling derived ctor.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker