C# formatter — re-indent C# with Allman braces

Paste C# code and get it re-indented from its braces, with each opening brace moved to its own line in the usual .NET style and string literals left exactly as written.

Live demo · real result from this tool
Indent

Formatted C#

using System;namespace Demo{    public class Invoice    {        public decimal Total { get; set; }        public string Describe()        {            if (Total > 1000m)            {                return $"Large invoice: {Total:C}";            }            else            {                return @"Small invoice";            }        }    }} 

Indentation and spacing were normalised by brace structure — this is not a full C# formatter, so line wrapping, blank-line rules and token spacing beyond the basics are left as written.

Processed on your device — nothing is uploaded.

How to use the C# formatter

  1. Paste C# into the Input box. The sample is an Invoice class with no indentation.
  2. Choose the Indent (4 spaces by default) and Opening braces, which defaults to Own line (Allman / Microsoft style).
  3. Keep Normalise spacing on to get if (, foreach ( and using (, and set Max blank lines in a row.
  4. 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:

  1. 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.
  2. Depth is counted from open braces, brackets and parentheses, with case labels one level inside switch.
  3. Spacing is added after keywords like if, foreach, using, lock and catch, 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?
No. Those use the Roslyn compiler and your .editorconfig rules, and they also fix operator spacing and wrapping. This tool rebuilds indentation from braces and moves braces, which is enough to make pasted or generated code readable again. Use dotnet format in your repository.
Why does the opening brace go on its own line?
Own line (Allman) is the default because it matches the Microsoft C# coding conventions and the Visual Studio defaults. Choose Same line if your team writes K&R style, or Keep as written to change indentation only.
Are verbatim and interpolated strings safe?
Yes. @"…" verbatim strings, $"…" interpolated strings, """ raw string literals, char literals and comments are masked before braces are counted. A brace inside a string never changes the indentation, and lines inside a multi-line verbatim string are copied unchanged.
How are region and preprocessor lines placed?
Region and endregion lines are indented with the code around them. Other directives such as #if, #else and #endif stay at the left margin, as Visual Studio places them.
What does it do with object initialisers and properties?
A one-line auto property like public decimal Total { get; set; } stays on one line. A multi-line object initialiser has its opening brace moved to its own line and its members indented.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close