Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!