"Use object initializer" breaks target-typed new expressions


ReSharper's "use object initializer" action removes the parentheses from a C# 9 target-typed new expression, rendering it invalid.

Start with:

Foo x = new();
x.Bar = 42;

Invoke the "use object initializer" action, which produces:

Foo x = new { Bar = 42 };

The entire expression is then marked as an error: "Cannot implicitly convert type <anonymous type int Bar> to type Foo".

Target-typed new expressions require the parentheses. R# should not be removing them. The correct code is:

Foo x = new() { Bar = 42 };

R# 2020.3.3, VS2019 v16.8.6

1 comment
Comment actions Permalink

Hello Richard Deeming,

Thanks for the bug report! The problem has been fixed, the fix will be available in R# 2021.1.1 which we're going to release in a month.

Have a great day!


Please sign in to leave a comment.