With the release of Visual Studio 2017 and updates, code refactoring helpers of C# projects are strongly improved and the refactoring activities has become easier. The intellisense has default rules that, normally, are the most used and recommended for the developers, but fortunately we have the ability to change these settings as we prefer. We can choose default settings for a solutions, thus all the developers will have the same settings for the same project.
In this post we’ll discover the C# code style options and how we can customize those.
Code style options
We can find the C# code style options under Tools > Options > Text Editor > C# > Code Style:
By clicking to an option, we can see a useful preview at the bottom of the window, where we can understand the effects of the settings. In the Preference column we can choose if we want to activate the option and in the Severity column the level of the message:
We can choose one of these levels:
- Refactoring Only
As you can imagine, the options are ordered from the lower to the higher level. With Refactoring Only we’ll see the option with Alt+Enter on the piece of code that we want to refactor.
With Suggestion, Visual Studio will show an indicator:
By setting the Warning option, we’ll have an indicator with major severity:
And obviously, with error we’ll have an error at compile time.
By looking on the possible options, we have a lot of flexibility about what we can choose and all the choices are grouped by categories:
With the release of Visual Studio 15.8 we have new options, like Prefer conditional expression over ‘if’ with assignment:
Another option about C# 7 is Prefer pattern matching, where we can choose if we want to apply the new pattern matching statement.
We have more inline option as well, like the Invert If:
In some contexts this operation can simplify the syntax and make the code more readable. Another new feature introduced is adding a new parameter to a method from the caller:
This is a very helpful functionality that help the refactoring activity.
All of this settings can be customized as personal settings of our Visual Studio installation or as general settings of the application. We can do that by define a configuration file.
Editor configuration file
In order to add default settings for a solution we need simply right click on the solution name in the solution explorer and add a new txt file named .editorconfig. Then we can insert our preferences like this:
root = true [*.cs] dotnet_style_qualification_for_field = true:suggestion
We can specify the file extentions on which we want to apply the rules and then define the settings. Every setting is identified by a name and we can define if the setting is enable and the level of the message by using the two points character as separator.
All the settings defined in this file will be automatically applied to the solution for all developers, mantaining the settings consistent across different workstations.
Visual Studio 2017 offer multiple options to customize C# code style as we prefer. With Visual Studio options we apply customizations at workstation level, recommended if we work alone on projects. Otherwise we can add the .editorconfig file to the solution and share/define the settings for all the project developers.
We have seen other features like InverIf and how add a new parameter to a method from a caller, tools that improve productivity and speedup the daily work.