Skip to content

Adjusting namespace when moving file deletes explicit anonymous type member names #80133

@zdenek-jelinek

Description

@zdenek-jelinek

Version Used: 17.14.13 (August 2025)

Steps to Reproduce:

  1. Have code in a file where anonymous type is constructed using explicit names even if they could be inferred

    using System;
    
    namespace TestConsoleApp.Original;
    
    public class AnonymousTypeUsage
    {
        public object MakeAnonymousTypeValue()
        {
            return new { Now = DateTime.Now };
        }
    }
  2. Ensure that anonymous type member name inference is not preferred
    Image

  3. Move the file to a different namespace and confirm the 'Adjust namespaces for moved files' dialogue

Diagnostic Id: IDE0037 Use inferred member name

Expected Behavior:
The namespace should be updated without preferably changing anything else in the file. But if it does change something, it should be according to the style rules.

Actual Behavior:
The explicit anonymous type names are gone (comment mine):

using System;

namespace TestConsoleApp.Different;

public class AnonymousTypeUsage
{
    public object MakeAnonymousTypeValue()
    {
        return new { DateTime.Now }; // <-- 'Now = ' was deleted
    }
}

Additional remarks:
The Now property is shown as greyed-out and suggesting a removal even with the Visual Studio setting set to 'No':
Image

Note that there is no .editorconfig anywhere that I know of, but if I add one with

[*.cs]
dotnet_style_prefer_inferred_anonymous_type_member_names = false

the property is no longer greyed out and removal of the member name is no longer offered:
Image

Moving the file and adjusting namespace still removes the member name, though, regardless of the .editorconfig setting. It would be nice if the setting worked consistently in VS, but my main issue is the unprompted removal when updating namespaces for moved files.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions