CSharp Formatter
Format and beautify C# code online with customizable indent size, brace style, and spacing rules. Instant, client-side CSharp formatter.
About
Inconsistent C# formatting degrades code review velocity and introduces merge conflicts that cost engineering teams measurable hours per sprint. Style drift across files, where one developer uses Allman braces and another uses K&R, creates cognitive overhead that compounds with codebase size. This formatter applies deterministic rules to raw C# source: it normalizes indentation depth, enforces brace placement style, collapses redundant blank lines, trims trailing whitespace, and inserts operator spacing per configurable policy. It processes the input entirely client-side through a lexical tokenizer that recognizes C# constructs including verbatim strings (@"..."), interpolated strings ($"..."), preprocessor directives (#region, #if), LINQ queries, and XML doc comments.
The tokenizer operates in O(n) time where n is character count. It does not parse a full AST. This means it handles syntactically broken code gracefully but will not reorder members or refactor logic. Limitation: deeply nested generic types with lambda expressions inside attributes may produce suboptimal spacing. For production codebases, pair this tool with .editorconfig enforcement in CI.
Formulas
The formatter operates as a finite-state tokenizer followed by a rule-based emitter. The tokenizer classifies each character sequence into a token type T from the set:
Indentation depth d is tracked as a counter incremented on BRACE_OPEN and decremented on BRACE_CLOSE:
The emitted line prefix is computed as: prefix = indentChar × (d × indentSize), where indentChar is either a space (U+0020) or tab (U+0009). Time complexity is O(n) for a single pass. Space complexity is O(n) for the output buffer.
Reference Data
| Setting | Option A | Option B | Default | Notes |
|---|---|---|---|---|
| Indent Style | Spaces | Tabs | Spaces | Tabs render at editor-defined width |
| Indent Size | 2 | 4 / 8 | 4 | .NET convention uses 4 |
| Brace Style | Allman (next line) | K&R (same line) | Allman | Allman is C# community standard |
| Max Blank Lines | 1 | 2 / 0 | 1 | Collapses consecutive empty lines |
| Trailing Whitespace | Trim | Keep | Trim | Always trim for clean diffs |
| Keyword Spacing | if (x) | if(x) | Spaced | Applies to if, for, while, switch, catch |
| Operator Spacing | x = 1 | x=1 | Spaced | Assignment, comparison, arithmetic |
| Comma Spacing | a, b | a,b | Spaced | Space after comma only |
| Semicolon Newline | Yes | No | Yes | For-loop headers excluded |
| Empty Body Style | { } | {} | Spaced | For empty methods/classes |
| Preprocessor Indent | None (col 0) | Match scope | None | Standard is column 0 |
| Using Sort | Alphabetical | Original | Original | System namespaces first when sorted |
| File Size Limit | 500 KB | - | Client-side memory constraint | |
| Line Ending | LF | CRLF | LF | Unix-style recommended for Git |
| Final Newline | Insert | Skip | Insert | POSIX compliance |