C# Code Formatter (.NET Style with .editorconfig)

Format C# files the way dotnet format does by default, sort using directives with System first, and read or write the matching .editorconfig settings.

Runs in your browser. Your files never leave your device. See how to check

…

How to do it

  1. 1
    Add .cs files

    Paste a class, open several files, or start from the samples.

  2. 2
    Pick a preset

    Use the .NET default, types and methods only, or K&R.

  3. 3
    Import .editorconfig

    Paste or open your .editorconfig to apply its C# formatting keys.

  4. 4
    Review

    Compare before and after, and read the issues for each file.

  5. 5
    Apply

    Apply the result to one file or all of them, with undo.

  6. 6
    Export

    Download the code, a ZIP, a .patch, or the matching .editorconfig.

About this tool

C# projects usually follow the .NET defaults that Visual Studio and dotnet format apply: four-space indentation, every opening brace on a new line, else, catch and finally on their own lines, a space after if, for and while, and using directives sorted with System namespaces first. Those defaults are stored as EditorConfig keys such as csharp_new_line_before_open_brace and dotnet_sort_system_directives_first, and this tool reads and writes exactly those keys. The tokenizer handles every C# literal form, including verbatim strings with doubled quotes, interpolated strings whose holes contain other strings or ternaries, raw string literals with three or more quotes, char literals, @keywords used as names and preprocessor lines. On top of the tokens, the formatter re-indents namespaces, types, members, switch sections and object initializers, moves braces to the chosen style, and spaces lambdas, null-coalescing operators and ternaries. It keeps named arguments such as name: value tight and recognises int? and obj! as nullable and null-forgiving rather than as operators. File-scoped namespaces are supported, and #region lines stay at the indentation of the code around them while #if lines go to column zero. Three presets cover the common choices: the .NET default with braces on new lines everywhere, braces on new lines for types and methods only, and K&R style with attached braces.

Checks and settings

Each file is checked for unbalanced brackets, unterminated strings, #region and #if directives without a matching end, and async void methods that are not event handlers, a pattern that hides exceptions from callers. The .editorconfig section exports your current options as a [*.cs] section you can commit to the repository, and it imports an existing file, reading only the keys under sections that match .cs files.

Worked example

The sample OrderProcessor.cs has 31 lines written in K&R style with unsorted usings. With the .NET default preset it grows to 39 lines: 9 lines change, 11 are added and 3 removed. The five using directives come out as System, System.Collections.Generic, System.Linq, Microsoft.Extensions.Logging and MyShop.Models, followed by a blank line. The namespace, class and method braces move to their own lines, "}else{" is split into a closing brace, else, and an opening brace, and the braces of the switch expression in Describe move under it. The interpolated string with a verbatim string inside its hole is unchanged, and the #region lines stay indented with the class members. With the K&R preset and separate import groups, the same file has 34 lines and blank lines between the System, Microsoft and MyShop groups.

Limits

This is not Roslyn: the built-in engine works on tokens and keeps your line breaks, so it will not wrap long argument lists or add braces to single-line if statements, and some rare layouts such as LINQ query clauses keep your own indentation. The optional clang-format build can format C# with its own rules but it is not identical to dotnet format either.

Worked example

OrderProcessor.cs with .NET defaults: 31 lines become 39, System usings first, Allman braces, else on its own line.

Quick facts

Frequently asked questions

Which .editorconfig keys does the tool understand?

indent_style, indent_size, max_line_length, csharp_new_line_before_open_brace, csharp_new_line_before_else, csharp_indent_switch_labels, csharp_space_after_keywords_in_control_flow_statements, dotnet_sort_system_directives_first and dotnet_separate_import_directive_groups.

How are using directives sorted?

global using lines first, then System and System.* namespaces when that option is on, then the rest alphabetically, then using static, then aliases. Duplicates are removed.

Can I keep braces on the same line?

Yes. Choose the K&R preset or set csharp_new_line_before_open_brace to none. The types and methods preset puts only type and member braces on new lines.

Are verbatim, interpolated and raw strings safe?

Yes. They are read as single tokens, including holes that contain other strings, so their content and line breaks are never touched.

Does it check for errors?

It reports unbalanced brackets, unclosed strings and directives, and async void methods. It does not compile the code, so type errors are not found.

Does my code leave the browser?

No. All formatting happens on this page, and files stay in your browser's local storage until you clear them.

Guides for this tool

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages