How to configure Visual Studio naming convention of private fields

Cosmin Vladutu
2 min readFeb 14, 2023

--

Those who worked with me, know, that I don’t like using this for privates. I think it’s a JS convention that should have remained there. I know that Microsoft preferred that way long ago, and StyleCop standard rules were with underscore/underline, and now even Microsoft devs prefer this way and stopped using this (if you check in their repos), but in reality, this is only a code styling flavour, and each team should decide for themselves how to do it.

To be more clear about what we’re talking about:

Using the “this” keyword for the private field

versus

Using underscore for the private field

I always searched for a way to make the IDE “help” in the way of suggesting to use “my preferred way” of working with private fields, and since my IDE is Visual Studio, I want to show you also how I did it.

You need to go in your Visual Studio to Tools -> Options -> Text Editor -> C# -> Coding Style -> Naming -> Manage naming styles and you should create a new entry (example below).

How to create a naming style.

The next step is to click on the Plus sign, near Manage specification (on the same menu, which you can also see in the picture), and apply the new “naming style” to “private or internal fields”. In my case the “end result” looks like this:

Added rule for “private or internal field”

The “thing” that I added, is the last line.
After this last step, you need to restart your Visual Studio and that’s it. You will see the new change applied in the next example:

Enjoy it, and I hope it will help you!

--

--

Cosmin Vladutu
Cosmin Vladutu

Written by Cosmin Vladutu

Software Engineer | Azure & .NET Full Stack Developer | Leader

No responses yet