C# Beautifier

Give a pasted C# file consistent four space indentation, with verbatim strings and switch labels handled correctly.

C# source
Formatted C#
Runs locally in your browser

About the C# Beautifier

C# code copied out of a log viewer, a chat window or a decompiler usually keeps its tokens but loses its shape. This beautifier rebuilds the shape from the brace structure. Namespaces, classes, methods, property bodies and object initialisers each add a level, and a line that begins with a closing brace steps back before it is printed.

Two C# specific details are handled. Verbatim strings written as @"C:\logs\app" are read as a single literal, including doubled quote escapes, so a backslash or a brace inside a path never affects the depth count. Switch sections are printed with case and default one level inside the switch and their statements one level deeper again, which is what the default Visual Studio setting produces.

Decimal and other suffixed literals such as 0m, 1.5f and 10_000L are copied byte for byte. That sounds obvious, but it is exactly where general purpose JavaScript beautifiers break C#: they split the suffix off the number and the file stops compiling. Nothing here rewrites a token.

The default indent is four spaces because that is the C# convention. Interpolated strings, attributes in square brackets and expression bodied members all pass through with their content intact. For turning a JSON payload into a C# class instead of formatting one, see the JSON to C# converter.

How to use

  1. Paste a .cs file into the left pane, or press Sample to load a small class with a property and a loop.
  2. Leave Indent on 4 spaces for standard C# style, or switch to tabs if your team uses them.
  3. Turn Split statements onto new lines off when you only want the leading whitespace corrected.
  4. Use Download to save the file as Formatted.cs.

Common questions

Does it break auto properties such as { get; set; } onto separate lines?
Yes, when statement splitting is on, because the accessors are separated by semicolons. Turn the option off to keep them on one line.
Are attributes like [HttpGet] kept above the method?
They keep whatever line they were written on and are indented to the level of the member below them.
Will file scoped namespaces work?
Yes. A namespace declared with a semicolon rather than a block simply adds no level, so the types below stay at the left margin.
Does it touch my LINQ query syntax?
Only the leading whitespace. Query clauses stay on the lines you wrote them on because no expression is rewrapped.