C# 9 new() syntax and private constructors lead to reported code smell/bug

When using the C# 9 new() syntax ( Target-typed new) with private constructors, Sonarcloud reports one of the following issues:

Code Smell: Remove the unused private constructor Foo

public class Foo
    {
        private Foo()
        {
        }
        public static Foo Create()
        {
            return new();
        }
    }

Bug: This class can’t be instantiated; make its constructor ‘public’.

public class Foo
{
    public static readonly Foo Instance = new();
    private Foo()
    {
    }
}
1 Like

Hi @tsc_templafy,

and thanks for the feedback.

We are currently aware of this issue (we have a test case documented here) and we do plan to improve our support for the new features added in C# 9 in the following months.

Best,
Costin