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