C# Formatter
Paste C# code and get it back neatly indented and spaced, in the Allman brace style Visual Studio uses or in compact K&R style. Choose indent size and line width; the built-in formatter runs as you type, and clang-format compiled to WebAssembly is available too. Nothing is uploaded.
Last updated:
Tip: press Ctrl + Enter to format; Tab indents inside the editor (press Esc first to move focus). Your code is formatted in your browser and is never uploaded, compiled or run.
How to use the C# Formatter
- Paste your C# code into the left editor, open a .cs file, or load a sample.
- Choose the brace style: Allman (new line, the .NET default), K&R (same line) or Stroustrup.
- Set indentation and line width; 4 spaces and 120 columns match common .NET settings.
- Check the result on the right, which updates as you type, or press Format C# (Ctrl + Enter).
- Copy or download the formatted code as a .cs file.
C# layout settings and their .editorconfig names
If your project uses an .editorconfig file, these are the settings that correspond to the options in this tool, so Visual Studio, Rider and dotnet format produce the same layout:
| Option in this tool | .editorconfig setting | .NET default |
|---|---|---|
| Indentation: 4 spaces | indent_style = space, indent_size = 4 | 4 spaces |
| Brace style: Allman | csharp_new_line_before_open_brace = all | all (Allman) |
| Brace style: K&R | csharp_new_line_before_open_brace = none | - |
else / catch / finally on a new line | csharp_new_line_before_else, _catch, _finally = true | true |
| Indented case labels | csharp_indent_switch_labels = true | true |
| No space after a cast | csharp_space_after_cast = false | false |
Space after if, for, while | csharp_space_after_keywords_in_control_flow_statements = true | true |
| Line width | max_line_length (used by some tools) | not enforced |
How C#-specific syntax is formatted
- Auto-properties stay on one line:
public string? Name { get; set; }. Properties with accessor bodies are expanded into blocks. - Object and collection initializers written on one line stay inline with spaces inside the braces:
new Person { Name = "Ann", Age = 30 }; multi-line initializers keep one member per line. - Lambdas and LINQ:
=>gets a space on both sides; long query chains wrap before.Where,.Selectand similar calls, and breaks you already placed before.are kept. - Null operators stay tight where they belong:
s?.Length ?? 0,items?[0],int? count,name!.Trim(). - Named arguments and tuples:
Foo(name: "x")and(a: 1, b: "x")keep the colon attached to the name, while base lists and constraints get spaces:class A : B where T : class. - Strings: interpolated (
$"…{x}…"), verbatim (@"C:\temp") and raw ("""…""") literals are never modified.
Need model classes from a JSON payload first? Use JSON to C# Class, then paste the result here to match your brace style.
Frequently asked questions
Which brace style does C# normally use?
Allman style, with each opening brace on its own line, is the default in Visual Studio and in the .NET runtime coding guidelines (csharp_new_line_before_open_brace = all in .editorconfig). Some teams, especially in Unity and cross-platform projects, prefer K&R with braces on the same line.
Does formatting change how my C# code behaves?
No. Only whitespace and line breaks change. Identifiers, string literals (including interpolated, verbatim and raw strings), comments and preprocessor directives are kept exactly as written.
How does this compare to dotnet format or Visual Studio's Format Document?
dotnet format and Visual Studio use the Roslyn compiler and your .editorconfig rules, so they can also fix code style issues. This tool only handles layout (indentation, spacing, braces and line wrapping) but needs no project or SDK and works on snippets that do not compile.
What does the clang-format engine do?
clang-format is the LLVM code formatter, which also supports C#. It is loaded once as WebAssembly (about 2 MB) and uses the Microsoft base style with your indent, brace and width choices. If it cannot load, the built-in formatter is used automatically.
Why are case labels indented inside switch?
Visual Studio's default (csharp_indent_switch_labels = true) indents case labels one level inside the switch block and the statements under them one more level. The formatter follows that convention.
Are #region and #if directives handled?
Yes. #region and #endregion are indented with the surrounding code, while #if, #else, #elif, #endif, #define and #undef are placed at the start of the line, as Visual Studio does.
Is my code sent to a server?
No. Everything runs in your browser. Only the optional clang-format engine is downloaded from a public CDN when you select it; your code never leaves your device.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.