question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Object initializer block for target-typed new is not indented correctly in an expression-bodied method

See original GitHub issue

Version 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:

  1. 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
        };
}
  1. 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:

image

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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
PathogenDavidcommented, Sep 4, 2020

Thanks @alrz!

0reactions
PathogenDavidcommented, Sep 4, 2020

If it does, I don’t think it’s doing so intentionally. Definitely glad to see those two issues fixed though!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found