Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
How to change the code generation style in Visual Studio 2022 to use "_" instead of "this."?

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;
    }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

See this doc page for a run-down on how to create code style rules.

To enforce that all private fields must be camel-case with a leading underscore, try something like:

dotnet_naming_style.camel_case_leading_underscore.capitalization = camel_case
dotnet_naming_style.camel_case_leading_underscore.required_prefix = _

dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private

dotnet_naming_rule.private_fields_should_be_camel_case_leading_underscore.severity = warning
dotnet_naming_rule.private_fields_should_be_camel_case_leading_underscore.symbols = private_fields
dotnet_naming_rule.private_fields_should_be_camel_case_leading_underscore.style = camel_case_leading_underscore

You'll now get a warning if any private fields aren't in this style, and the code generation will also respect this:

Screenshot showing VS respecting this rule

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda