Auto-formatting C# code

January 5th 2024 C#

I'm a big fan of Prettier, and I'm using it for auto-formatting code in any web projects I'm working on. Unfortunately, it doesn't support C#. I've been writing about alternatives in the past, although they weren't exactly on the same level. Only recently I learned about CSharpier, and of course, I had to try it out.

It follows the philosophy of Prettier and it shows. It's easy to set up, it offers almost no configuration options, and it just works. I have it enabled now, whenever I'm writing C# code.

For personal use, it's most convenient to install the .NET tool globally:

dotnet tool install -g csharpier

And then install and configure the extension/plugin in the editors you are using:

  • In Visual Studio 2022, you install the extension and globally enable reformat on save: CSharpier settings in Visual Studio 2022

  • In Visual Studio Code, you install the extension and enable reformat on save in user settings. You'll most likely also have to choose CSharpier as the default formatter, because the C# Dev Kit extension also installs one. It's best to manually edit the settings.jsonfile file:

    {
      "editor.formatOnSave": true,
      "[csharp]": {
        "editor.defaultFormatter": "csharpier.csharpier-vscode"
      }
    }
    
  • In JetBrains Rider, you install the plugin, but unfortunately you can't enable reformat on save globally for all projects because the setting is project specific. You'll still have to enable it for each project separately: CSharpier settings in JetBrains Rider

Except in JetBrains Rider, this configuration will enable CSharpier by default for all your projects. Whenever you'll be writing C# code, it will be automatically formatted on save.

For projects with multiple developers, it's better to set up everything per project so that auto-formatting will work for everyone without additional configuration (as long as they have the extension/plugin installed in their editor).

To install the .NET tool locally, you must first initialize the manifest file in the repository root (unless you already have other local tools installed):

dotnet new tool-manifest

Then you can install the CSharpier tool:

dotnet tool install csharpier

Make sure to add the resulting .config/dotnet-tools.json file to source control.

You will also want to enable reformat on save at the solution/workspace/project level in all editors your team uses. Each editor saves the setting value to a local file which you can add to source control, so that the setting will be shared among all developers:

  • Visual Studio 2022: .vs/<SolutionName>/v17/csharpier.json
  • Visual Studio Code: .vscode/settings.json
  • JetBrains Rider: .idea/idea.<SolutionName>/.idea/CSharpierPlugin.xml

If you want to be extra certain that your files are properly formatted, you also have the option of:

  • formatting the code in Git pre-commit hook to format the code before committing the files,
  • validating the code formatting as part of your CI/CD pipeline to file the build if code is not formatted correctly.

By configuring Prettier for all our web projects, we effectively ended all arguments regarding code formatting. We haven't done this yet with CSharpier for all our C# projects, but I'm quite certain it will have a similar effect. But even for personal use, I like how CSharpier automatically reformats the code when a line becomes too long, or after I refactor the code. I simply don't need to worry about code formatting and indentation anymore.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License