Object initializer block for target-typed new is not indented correctly in an expression-bodied method
See original GitHub issueVersion Used:
Visual Studio 16.8.0 Preview 2.1 .NET SDK 5.0.100-preview.8.20417.9 Compiler version: ‘3.8.0-2.20418.7 (0276b9b7)’. Language version: 9.0.
Steps to Reproduce:
- Write an expression-bodied member that uses target-typed new and an object initializer:
public class Person
{
public string Name { get; set; }
public static Person Create(string name)
=> new()
{
Name = name
};
}
- Let the document auto-format or you manually invoke it by running Code Cleanup or with Edit > Advanced > Format Document
Expected Behavior:
The indentation remains the same as the code snippet is written above.
Actual Behavior:
The initializer block is indented one less than it should be:
Note that it works as expected for normal block-bodied methods and for expression-bodied methods using traditional new syntax.
This might be related to #47438, but I’m filing it separately since they’re two different language features.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Object initializer block for `with` statement on record is not ...
The initializer block is indented one less than it should be: image. Note that it works as expected for normal block-bodied methods.
Read more >Fixing indentation when object initializers have been used
1 Answer. Go into ReSharper / Options / Languages / C# / Formatting Style / Other and uncheck "Indent array, object and collection...
Read more >Target-typed new expressions - C# 9.0 draft feature ...
This feature specification describes the enhancements to 'new' where the target type can be inferred from the variable declaration.
Read more >Towards a Beginner-Friendly DotNet - The New Dev's Guide
My suspicion is that developers will ultimately have a better experience with file-scoped namespace definitions due to the reduced nesting ...
Read more >1. Introducing C# and .NET - C# 10 in a Nutshell [Book]
The not combinator can be used with the type pattern to test whether an object is (not) a type: if (obj is not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks @alrz!
If it does, I don’t think it’s doing so intentionally. Definitely glad to see those two issues fixed though!