C#idiomatic C#
ca1012fundamentals/code-analysis/quality-rules

Code analysis

CA1012: Abstract types should not have public constructors

Learn about code analysis rule CA1012: Abstract types should not have public constructors

CA1012 Design enabled by default in .NET 10: No
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

A type is abstract and has a public constructor.

By default, this rule only looks at externally visible types, but this is configurable.

Rule description

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

How to fix violations

To fix a violation of this rule, either make the constructor protected or don't declare the type as abstract.

When to suppress warnings

Do not suppress a warning from this rule. The abstract type has a public constructor.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

csharp
#pragma warning disable CA1012
// The code that's violating the rule is on this line.
#pragma warning restore CA1012

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

ini
[*.{cs,vb}]
dotnet_diagnostic.CA1012.severity = none

For more information, see How to suppress code analysis warnings.

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Design) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility, by setting the api_surface option. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

ini
dotnet_code_quality.CAXXXX.api_surface = private, internal

Example

The following code snippet contains an abstract type that violates this rule.

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1012-abstract-types-should-not-have-constructors_1.vbsnippet1

vb
Imports System

Namespace ca1012
    '<snippet1>
    ' Violates this rule      
    Public MustInherit Class Book

        Public Sub New()
        End Sub

    End Class
    '</snippet1>
End Namespace

Namespace ca1012_2
    '<snippet2>
    ' Violates this rule      
    Public MustInherit Class Book

        Protected Sub New()
        End Sub

    End Class
    '</snippet2>
End Namespace
csharp
// Violates this rule
public abstract class Book
{
    public Book()
    {
    }
}

The following code snippet fixes the previous violation by changing the accessibility of the constructor from public to protected.

csharp
// Does not violate this rule
public abstract class Book
{
    protected Book()
    {
    }
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1012-abstract-types-should-not-have-constructors_1.vbsnippet2

vb
Imports System

Namespace ca1012
    '<snippet1>
    ' Violates this rule      
    Public MustInherit Class Book

        Public Sub New()
        End Sub

    End Class
    '</snippet1>
End Namespace

Namespace ca1012_2
    '<snippet2>
    ' Violates this rule      
    Public MustInherit Class Book

        Protected Sub New()
        End Sub

    End Class
    '</snippet2>
End Namespace