VS 2017 (and maybe olders versions) gives me this handy little constructor shortcut to generate a private readonly
field and assign it.
Screenshot:
This ends up generating a private member called userService
and then assigns it with:
this.userService = userService;
This goes against the code style that I use which is to name all private members with a prefix _
resulting in assignment that should look like:
_userService = userService;
How can I make it so that VS obeys this code style rule with its code generation shortcuts?
This can be also achieved directly in Visual Studio. Just go to
Tools -> Options -> Text Editor -> C# -> Code Style -> Naming
.Restart Visual Studio
After that, when you apply the "Create and initialize field" refactoring, it will be named with a leading underscore.
This can be achieved by creating your own Roslyn Code Analyzer naming rule. Add a
.editorconfig
in your solution to specify custom naming conventions.Read more about them here: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
To get the desired effect from the question, the following will work:
Result:
The .editorconfig settings is kspearrin's answer didn't work for me I had to use these (for VS2017 Version 15.4.0):
I got these from here: https://github.com/dotnet/roslyn/issues/22884#issuecomment-358776444