Is there a pretty printer / code formatter for C# (as part of build system)?

Is there a pretty printer / code formatter for C# (as part of build system)? Read as: "lives outside of Visual Studio". It seems like there are plenty of these kinds of things for Java, C++/C, Go -- so it seems more than reasonable that C# should also have some code formatter that lives outside of the IDE?

(I'd actually like to couple the formatter with something like StyleCop, and have devs run it as part of the process before a commit).

NOTE: Not syntax highlighting, as in code in a web page. Instead, a Code beautifier, or code pretty printer, which would take code and format it to a style/standard coding format... which StyleCop does a style check to see if the code meets the format.


JetBrains also makes a free command line tool named CleanupCode that formats c# based on .editorconfig settings. I wrote a wrapper utility for it named ReGitLint that makes it easier and faster to run as a pre-commit hook or on the build server. This can help out a lot if you've got teammates using Visual Studio Code where ReSharper isn't an option.

To get up and going just run the following commands

dotnet tool install JetBrains.ReSharper.GlobalTools
dotnet tool install ReGitLint

then add the following to .git/hooks/pre-commit

#!/bin/sh
dotnet regitlint -f staged --fail-on-diff

To enforce formatting on jenkins add this to your build script

dotnet tool restore
dotnet regitlint --jenkins

For more options check out the readme


The first that comes to mind is ReSharper. However, it's an add-in to VS which isn't what you were asking for. I'm not sure if you can set it up to auto-format on save.

From their website:

ReSharper can reformat an arbitrary selected block of code, all code in the current file, all files in a directory, or even in the entire project or solution, according to your Code Style preferences. ReSharper provides distinct formatting options for C#, VB.NET, JavaScript, CSS and XML code. Reformatting affects braces layout, blank lines, line wrapping and line breaks, spaces in various contexts, indentation, aligning multiple constructs, and a lot more options that you can fine-tune in the ReSharper Options dialog box.