Home > Enterprise >  How to change the code generation style in Visual Studio 2022 to use "_" instead of "
How to change the code generation style in Visual Studio 2022 to use "_" instead of "

Time:03-24

I am using Visual Studio 2022 to write C# code.

When adding a property to the constructor, then click on "Quick action and refactoring" and select "create and assign field for 'session'" visual studio would create the following

public class ExampleClass
{
    private ISession session;

    public ExampleClass(ISession session)
    {
        this.session = session;
    }
}

How can I change that style to use _ instead of this.? so the generated code will then be

public class ExampleClass
{
    private readonly ISession _session;

    public ExampleClass(ISession session)
    {
        _session = session;
    }
}

CodePudding user response:

You can create a .editorconfig file, which specifies your code style preferences. The VS code generation will then respect that.

See Screenshot showing VS respecting this rule

  • Related