Rasmus Olsson

Stay productive with formatted code

February 25, 2022

It has always surprised me that the demand for a unified code format is not that desirable in dotnet. In Node with the support for eslint and prettier, setting up formatting and linting rules is almost mandatory. For dotnet, formatting and linting still seem to be optional in most developers setup.

The benefits of using a unified code format for a development department can be quite low-hanging fruit and increase productivity. Reading code that looks the same, aka has the same format, saves us time, reduces noise during code reviews, and lets us focus more on functionality and less on subjective rules.

In this post, I will go through how to set up dotnet format.

Determine the format rules

To determine what formatting rules to use at your company/team will probably be the most demanding step. Even though there are good defaults in dotnet format, there may still be some customizations that your department wants to make.

To add a dotnet format customization, you can create a .editorconfig.json file in your solution. dotnet format will then look for this file when formatting.

More on editorconfig.json can be read here:


Guard from unformatted code changes

When you have determined the rules to use, you would want to guard that no new unformatted changes make it into your main branch. This step may vary depending on your CI setup, but adding a separate linting step before you build your solution would probably be the most likeliest place.

To check the format, run the following command:

dotnet format --verify-no-changes

This will result in a non-zero status code if the solution isn't already formatted.

To make sure that CI has dotnet format available, you can use a tool-manifest file. This will also help in that dotnet format runs on the same version for local development.

You basically creates a manifest file and then run dotnet tool restore. More information about a local manifest file can be found here:


Run dotnet format

This step includes formatting the code. Before running dotnet format, it might be necessary to double-check that dotnet format is excluding folders you don't want to have formatted. In example generated files. You can use the --exclude parameter for that but other than that it should just be to run:

dotnet format

Run dotnet format on client commit hook

Depending on your development process you may want to run dotnet format on git commit. You can read more about that in my git hook for dotnet post here: https://www.rasmusolsson.dev/posts/git-hook-for-dotnet

That's pretty much it.

Happy Coding!

please share