How to use the C# formatter
- Paste C# into the Input box. The sample is an
Invoiceclass with no indentation. - Choose the Indent (4 spaces by default) and Opening braces, which defaults to Own line (Allman / Microsoft style).
- Keep Normalise spacing on to get
if (,foreach (andusing (, and set Max blank lines in a row. - Copy the Formatted C# from the result.
What it does and when to use it
C# snippets lose their shape when they pass through email, Teams, Stack Overflow comments or a decompiler’s output window. Unity scripts written in a hurry and code generated by templates often mix brace styles too. The formatter restores one level of indent per block and puts every opening brace in one consistent place.
The same engine powers the Java formatter and the C/C++ formatter, which default to their own languages’ brace habits. For the SQL inside your repositories, try the SQL formatter; for .csproj and app.config files, the XML formatter.
How it works
Each line is scanned first. Strings, verbatim strings (including @$"…"), raw string literals, char literals and comments are hidden so their contents cannot affect the result. Then:
- Braces move. In Allman mode, a
{at the end of a code line goes to the next line at the same indent, and} else {is split onto three lines. Braces after=,(or=>stay put. - Depth is counted from open braces, brackets and parentheses, with
caselabels one level insideswitch. - Spacing is added after keywords like
if,foreach,using,lockandcatch, before{after), and after commas.
Nothing else changes, and the code is not compiled or uploaded.
Worked examples
These are the tool’s own outputs with the default settings.
The sample class. It comes out in Allman style:
namespace Demo
{
public class Invoice
{
public decimal Total { get; set; }
Inside Describe(), } else { becomes }, else and { on three lines, and return @"Small invoice"; sits at its correct depth.
A verbatim SQL string. A two-line @"SELECT Id … FROM Orders -- {draft}" string is kept exactly, so the brace in it does not count, and the foreach loop after it is laid out normally:
foreach (var o in orders)
{
Console.WriteLine(o.Id);
}
The note reports “1 line(s) inside multi-line strings or templates were left exactly as they were”.
Regions and conditional code. Inside class Repo, #region Helpers and #endregion are indented with the members, while #if DEBUG and #endif stay at column 0.
Limits and tips
- No operator spacing.
return n*2;stays as typed. - No using sorting, no line wrapping, no blank lines added between members.
- Expression-bodied members (
=>) and LINQ chains keep your own line breaks.
Frequently asked questions
Is this the same as dotnet format or the Visual Studio formatter?
Why does the opening brace go on its own line?
Are verbatim and interpolated strings safe?
How are region and preprocessor lines placed?
What does it do with object initialisers and properties?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.