Design Warnings


Visual Studio Team System
Abstract types should not have constructors

TypeName

AbstractTypesShouldNotHaveConstructors

CheckId

CA1012

Category

Microsoft.Design

Breaking Change

NonBreaking

Cause

A public type is abstract and has a public constructor.

Rule Description

Constructors on abstract types can only be called by derived types. Because public constructors create instances of a type, and you cannot create instances of an abstract type, an abstract type with a public constructor is incorrectly designed.

How to Fix Violations

To fix a violation of this rule, either make the constructor protected, or do not declare the type as abstract.

When to Exclude Warnings

Do not exclude a warning from this rule.

Example

The following example contains an abstract type that violates this rule, and an abstract type that is correctly implemented.

C#
using System;

namespace DesignLibrary
{
   public abstract class BadAbstractClassWithConstructor
   {
      // Violates rule: AbstractTypesShouldNotHaveConstructors.
      public BadAbstractClassWithConstructor()
      {
      // Add constructor logic here.
      }
   }

   public abstract class GoodAbstractClassWithConstructor
   {
      protected GoodAbstractClassWithConstructor()
      {
         // Add constructor logic here.
      }
   }
}

The following example shows an abstract type that violates this rule.

In the example above abstract type has a public constructor, which can confuse users. They see the public constructor, but do not understand why they are unable to create the type.

The following example fixes the above violation by changing the constructor's accessibility from public to protected.

Tags :


Community Content

David M. Kean - MSFT
Example

The following example shows an abstract type that violates this rule.

[C#]
   
using System;
   
namespace Samples
{
// Violates this rule
public abstract class Book
{
public Book()
{
}
}
}

[Visual Basic]
 
Imports System
   
Namespace Samples
   
    ' Violates this rule
    Public MustInherit Class Book
   
        Public Sub New()
End Sub
   
    End Class
      
End Namespace 

[C++]
 
using namespace System;
 
namespace Samples
{
    // Violates this rule
public ref class Book abstract
{
public:
Book()
{
}
}
}

The above abstract type has a public constructor, which can confuse new users. They see the public constructor, but do not understand why they are unable to create the type.

The following example fixes the above violation by changing the constructor's accessibility from public to protected.

[C#]
   
using System;
   
namespace Samples
{
public abstract class Book
{
protected Book()
{
}
}
}  

[Visual Basic]
 
Imports System
   
Namespace Samples
   
    Public MustInherit Class Book
 
        Protected Sub New()
        End Sub
   
    End Class
   
End Namespace
 

[C++]
 
using namespace System;
 
namespace Samples
{
public ref class Book abstract
{
protected:
Book()
{
}
}
}
Tags : codeanalysis

Page view tracker