Unnecessary Default Constructor

ID

csharp.unnecessary_default_constructor

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

CSharp

Tags

code-style

Description

Reports a class whose only constructor is an explicit public parameterless constructor with an empty body. The C# compiler already supplies such a constructor automatically when a class declares none, so the hand-written one adds nothing.

Rationale

When a class declares no constructors, the compiler generates a public parameterless one. An explicit empty parameterless constructor duplicates that behavior, adding lines that a reader must scan past for no benefit. Removing it leaves the class behaving identically while reducing clutter. Constructors that take parameters, run code, chain with : this(…​), pass arguments through : base(args), or are non-public are left untouched because they do real work or deliberately constrain instantiation.

public class WithRedundant
{
    public WithRedundant()       // FLAW — equivalent to the implicit constructor
    {
    }
}

public class WithBaseCall
{
    public WithBaseCall() : base()   // FLAW — bare base() matches the implicit call
    {
    }
}

public class WithLogic
{
    private readonly int seed;

    public WithLogic()           // OK — initializes a field
    {
        seed = 42;
    }
}

public class WithParameter
{
    public WithParameter(int x)  // OK — not parameterless
    {
    }
}

Remediation

Delete the empty parameterless constructor and rely on the compiler-generated default constructor.

References