Avoid using a public contructor for an abstract class

Metadata

ID: csharp-best-practices/public-abstract-constructors

Language: C#

Severity: Notice

Category: Best Practices

Description

Using an abstract modifier in a class declaration indicates that a class is intended only to be a base class of other classes and not instantiated by itself. Due to this, there is no need for public or internal constructors within. Any initialization logic should be added in a private,private protected, or protected constructor.

Non-Compliant Code Examples

abstract class Foo
{
    internal Foo()
    {
      //...
    }
}
abstract class Foo
{
    public Foo()
    {
      //...
    }
}

Compliant Code Examples

abstract class Foo
{
    private protected Foo()
    {
      //...
    }
}
abstract class Foo
{
    private Foo()
    {
      //...
    }
}
abstract class Foo
{
    protected Foo()
    {
      //...
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: may/unit-testing