5 minute read

The C# formatting tool I like the most right now is csharpier.

In a few previous posts on this blog I have written about formatting tools, but I have never been totally happy. A new contender has been on my radar for a while, and in this post I’ll show you how to use it.

CSharpier is a commandline utility to reformat C# code. Nothing more. And its a good step on the way to production ready. At the time of writing its just released v0.15.0, with new addons for all the major C# IDEs! Visual Studio, VS Code and Rider can now have the same formatting!

CSharpier is inspired by prettier, an almost ubiquitous tool in the JavaScript / TypeScript world. Almost zero config, so every project is the same. Its much easier to read, understand and write code when everything is similar.

In this post I’ll show you how to use it in your C# project. Its easy to sync across all team members, and to integrate in CI pipelines.

Prerequisites

Install csharpier in your dotnet project as a tool

# Change terminal folder to the solution folder

# new tool-manifest creates a json file where dotnet can find
# which version of a tool should be used for this solution.
# This helps avoid your team having different versions of a
# formatter for example
dotnet new tool-manifest

# Install csharpier to this project
dotnet tool install csharpier

Using csharpier

Tip: Ensure you have checked in any changes to git before running csharpier the first time. Even though you thought your code was nicely formatted, there will be changes in most files.

To format all files in the current project:

# The . is "current folder", so you should run this from your
# project/solution root
dotnet csharpier .

This will usually take a second, and after that all files are formatted. You might not agree with all of these, but the magic feeling comes when you apply this on “save” in your IDE.

If you want to format just a single file, replace the . with a path to a file.

Ignoring files

If there is stuff you want to NOT format, its possible to create a .csharpierignore file. It works kinda like a gitignore (and has the same syntax), you specify a path, and it will be ignored from the formatting.

See more tips for ignoring files here

Using csharpier in your IDE

This post will be outdated in a few weeks with the current pace of csharpier, so I’ll redirect you to the official docs for the addons: https://csharpier.com/docs/Editors

The important step is to always configure it to run on save. If you do not do this, it will feel more like a chore and less like magic.

Enforcing csharpier in your CI pipeline

I also recommend you check the offical docs here, but the quick gist of it is that you have to run these two commands.

dotnet tool restore # This restores the tool-manifest from above
dotnet csharpier . --check # This runs csharpier, and if it fails it will exit with a non-zero exit code and fail the step.
steps:
  # // Snip
  - name: "Check for style issues"
  - run: |
      dotnet tool restore
      dotnet csharpier . --check

Finale

I used csharpier in the StereoKit Hackathon project, with great success.

I really recommend trying this out if you want a consistently formatted solution. Fast, easy and free as in Open Source (and beer 🍻).

Thats all for this time.

// Nils Henrik

This post was updated to fix broken links on 2023-06-26. I can still highly recommend csharpier as of v0.25.0!