224

VS 2017 (and maybe olders versions) gives me this handy little constructor shortcut to generate a private readonly field and assign it.

Screenshot:

enter image description here

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?

4
  • This doesn't have anything to do with snippets, this feature is provided by the code analysis service. Available since Roslyn was added. A leading underscore is a violation of the .NET Framework Programming Guideline, no way that you are going to talk a Microsoft programmer into changing this. It is a style pushed by Resharper, they don't like this. because that makes programmer not buy it, consider using it. Aug 19, 2017 at 22:48
  • 35
    @HansPassant Every piece of modern code I have seen produced by the teams at Microsoft follow this _camelCase convention for private members. See the CoreFX C# Coding Style Guidelines, ASP.NET Core Coding Style and even Rosyln code itself...??
    – kspearrin
    Aug 19, 2017 at 23:43
  • 12
    @HansPassant Good news ... someone successfully talked a Microsoft programmer into being able to change this. See the answer below.
    – kspearrin
    Aug 23, 2017 at 17:27
  • Any idea how to do this in Visual Studio for Macs?
    – Jason V
    Aug 29, 2019 at 20:31

4 Answers 4

452

This can be also achieved directly in Visual Studio. Just go to Tools -> Options -> Text Editor -> C# -> Code Style -> Naming.

  1. Firstly you need to define a new naming style by clicking the "Manage naming styles" button:

VS2017 Naming style dialog

  1. Then click the + sign to define a new rule for "Private or Internal Field", that uses your new naming style:

VS2017 Options dialog

  1. Restart Visual Studio

  2. After that, when you apply the "Create and initialize field" refactoring, it will be named with a leading underscore.

11
  • 1
    I'm on Visual Studio 15.8.8 and this was the only answer here that worked for me. Nov 4, 2018 at 16:49
  • 39
    FYI: You may have to restart VS after this.
    – Jeff
    Dec 11, 2018 at 20:23
  • 10
    Works well in VS 2019. Jun 11, 2019 at 20:33
  • 4
    Not all heroes wear capes. Great explanation. Works great! Thx.
    – datoml
    Nov 22, 2019 at 10:05
  • 11
    No restart required in VS 2019 Version 16.5.4 May 19, 2020 at 23:28
100

The .editorconfig settings is kspearrin's answer didn't work for me I had to use these (for VS2017 Version 15.4.0):

[*.{cs,vb}]
dotnet_naming_rule.private_members_with_underscore.symbols  = private_fields
dotnet_naming_rule.private_members_with_underscore.style    = prefix_underscore
dotnet_naming_rule.private_members_with_underscore.severity = suggestion

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

dotnet_naming_style.prefix_underscore.capitalization = camel_case
dotnet_naming_style.prefix_underscore.required_prefix = _

I got these from here: https://github.com/dotnet/roslyn/issues/22884#issuecomment-358776444

9
  • Weird. I am now using VS 2017 15.6 and my original answer still seems to work there. Who knows...
    – kspearrin
    Mar 22, 2018 at 13:24
  • I updated to 15.6.3 and this version stills works for me. I'm using Community edition, maybe you're not? Mar 22, 2018 at 14:20
  • 1
    following this also: learn.microsoft.com/en-us/visualstudio/ide/…
    – user117499
    Mar 27, 2018 at 18:35
  • 3
    Unfortunately, the above setting doesn't work in the web project.
    – Vincent
    Jul 5, 2021 at 23:11
  • 1
    For some reason, it's also applied to const? Nov 30, 2021 at 14:26
41

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://learn.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference

To get the desired effect from the question, the following will work:

[*.{cs,vb}]
dotnet_naming_rule.private_members_with_underscore.symbols  = private_fields
dotnet_naming_rule.private_members_with_underscore.style    = prefix_underscore
dotnet_naming_rule.private_members_with_underscore.severity = suggestion

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

dotnet_naming_style.prefix_underscore.capitalization = camel_case
dotnet_naming_style.prefix_underscore.required_prefix = _

Result:

enter image description here

7
  • This seems to me to be the long way of accomplishing this task. Please see @Maciek 's response.
    – Eric
    Jan 17, 2019 at 21:22
  • This was the only way back when I originally answered. Looks like they added a UI for it now.
    – kspearrin
    Jan 17, 2019 at 22:52
  • 7
    Editing .editorconfig is a much nicer solution than doing it via the UI.
    – gliljas
    Mar 25, 2020 at 16:37
  • 19
    @Eric It's the other way around. Having to tell every member of your team to configure their VS a certain way by hand is definitely much longer, tedious, and error prone than checking in an .editorconfig file in Git and knowing that from that moment on everyone will have their settings automatically adjusted to follow the project's coding style. Jun 17, 2020 at 22:21
  • 1
    Answer addressing VS2022 from @aleksander_si provided insight that the readonly required modifier was preventing the Create and initialize field from working for me.
    – Rock
    Mar 6 at 17:01
10

I read the previous solutions and as much as they seem correct, the only way to get the configuration to work correctly in VS2022 is to properly order the .editorconfig entries, as follows:

# Use underscores for private fields
[*.{cs,vb}]
dotnet_naming_symbols.private_fields.applicable_kinds           = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private

dotnet_naming_style.prefix_underscore.capitalization = camel_case
dotnet_naming_style.prefix_underscore.required_prefix = _

dotnet_naming_rule.private_fields_with_underscore.symbols  = private_fields
dotnet_naming_rule.private_fields_with_underscore.style    = prefix_underscore
dotnet_naming_rule.private_fields_with_underscore.severity = warning
3
  • See stackoverflow.com/a/52704752/3568845 to keep const in Pascal case.
    – Alix
    Mar 25 at 8:16
  • This worked great thanks!
    – Rick Glos
    Sep 28 at 21:45
  • This was picked up immediately by Visual Studio 2022. I want to have underscore when I perform a dependency injection in constructor. I was editing the .editorconfig in VsCode and VS 2022 saw my change at once. I also avoided the annoying this. prefix that VS usually adds in front inside constructor. Put aleksander_si block at the top of naming rules at the top of the file Nov 21 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.