Visual Studio Code Style Settings

In Visual Studio there is a mechanism to establish a sole code style using automatically sent notifications, warnings, or even errors. If you do not want to constantly monitor curly braces manually, you can simply set up the development environment once and use it all the time. Why is this necessary? A good code, first of all, should be readable, and a single style helps to achieve this.

You can fine-tune your code style preferences by opening Tools > Options window, and then selecting Text Editor > C# / Basic > Code Style. There you can change code preferences for 'this' and 'var' keywords, predefined types, curly braces, expressions and 'null' checking.

Tools > Options > Text Editor > C# / Basic > Code Style

Also, you can see that we are able to set severity for each rule. On the official Microsoft website, it is said that:

Severity can be set to None, Suggestion, Warning, or Error, and Visual Studio will behave appropriately. If you want to use Quick Actions with these code styles to automatically rewrite code to the preferred style, ensure that the setting is set to something other than None so the light bulb icon will appear when the non-preferred styles are used. These preferences can then can be applied by clicking the Light Bulb icon or pressing Ctrl + . when your cursor is on the appropriate line of code.

 As for me, the only rule I have set not to prefer is 'this' keyword. I commonly write local variables from small letter, public properties from capital and private fields and properties from underscore. It helps me to clearly see the scope for every member, thereby preventing ambiguity and making usage of 'this' keyword a redundancy.

Talking about severity, it is up to you what to choose. I only want to mention one important drawback of setting it to 'Warning'. Consider a situation when you have to review someone’s code or just want to look at it. You open that project, everything is fine. You try building it, when suddenly dozens of errors appear because of “wrong” code style, and you are no longer able to run the application. Awkward situation. To avoid it, I have set everything to 'Suggestion'.

In conclusion, I would like to say that the code style described in this entry is just my personal preference and do not pretend to be the correct one. And, of course, it is up to team members what code style to use in this particular project and whether to unify it at all.