C# Formatter

Re-indent C# code by brace depth.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What the C# Formatter does

The C# Formatter re-indents C# code: each line is placed four spaces in for every unclosed {, ( or [ before it. It tidies code whose indentation was lost when it was pasted or copied from a chat or a PDF.

It reads C# strings, character literals, comments and verbatim strings (@"…"), interpolated strings ($"…{x}…") and raw string literals (""" … """), so a brace inside them does not change the indentation, and lines inside a multi-line string are left exactly as written. It does not rewrap lines or change spacing within a line.

C# Formatter examples

The built-in sample

Input

public class Greeter
{
public string Name { get; set; }
public string Greet(int n)
{
if (n > 1)
{
return "Hello all";
}
return $"Hello {Name}";
}
}

Output

public class Greeter
{
    public string Name { get; set; }
    public string Greet(int n)
    {
        if (n > 1)
        {
            return "Hello all";
        }
        return $"Hello {Name}";
    }
}

This is the input that the Load sample button fills in.

Braces inside strings and comments

Input

class Report
{
string Title => $"{Name} {{draft}}";
string Path = @"C:\{data}";
void Print()
{
Console.WriteLine(Title);
}
}

Output

class Report
{
    string Title => $"{Name} {{draft}}";
    string Path = @"C:\{data}";
    void Print()
    {
        Console.WriteLine(Title);
    }
}

The braces inside strings and comments are ignored, so the following lines are indented correctly.

Each output was produced by running this tool on the input above it.

How it works

Each line is trimmed and indented by the number of brackets ({, ( and [) still open before it. A line that starts with a closing bracket moves out one level first, so } else { lines up with its if.

While reading, the tool skips over strings and comments using the C# rules. Lines that begin inside a multi-line string are copied unchanged, because changing them would change the string. Lines inside a block comment are re-indented, and a leading * is lined up under the opening /*. Runs of blank lines become one.

Details and limits

  • Your line breaks are kept: code that is all on one line comes back on one line.
  • The tool does not check syntax. An unclosed string or bracket shifts the lines after it, which is often a quick way to find one.
  • For a full reformat, with line wrapping and spacing rules, use dotnet format or your IDE.

Common errors

SymptomWhat it means
Everything after a line is indented too farA bracket or string on that line is not closed. Check the line where the indentation starts to drift.

Questions

Is my data uploaded when I use C# Formatter?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does C# Formatter do?

The C# Formatter re-indents C# code: each line is placed four spaces in for every unclosed {, ( or [ before it. It tidies code whose indentation was lost when it was pasted or copied from a chat or a PDF.

How does C# Formatter work?

Each line is trimmed and indented by the number of brackets ({, ( and [) still open before it. A line that starts with a closing bracket moves out one level first, so } else { lines up with its if.

Are there any limits I should know about?

Your line breaks are kept: code that is all on one line comes back on one line. The tool does not check syntax. An unclosed string or bracket shifts the lines after it, which is often a quick way to find one.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.