User Rating 0.0
Total Usage 0 times
Input 0 chars · 0 lines
Output 0 chars · 0 lines
Is this tool helpful?

Your feedback helps us improve.

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.

csharp formatter c# beautifier code formatter csharp beautify c# code formatting csharp pretty print code indentation

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:

T { KEYWORD, IDENT, STRING, CHAR, COMMENT_LINE, COMMENT_BLOCK, PREPROCESSOR, OPERATOR, BRACE_OPEN, BRACE_CLOSE, PAREN, SEMICOLON, WHITESPACE, NEWLINE, NUMBER }

Indentation depth d is tracked as a counter incremented on BRACE_OPEN and decremented on BRACE_CLOSE:

dn+1 = dn + 1 if token = { dn+1 = max(0, dn 1) if token = }

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

SettingOption AOption BDefaultNotes
Indent StyleSpacesTabsSpacesTabs render at editor-defined width
Indent Size24 / 84.NET convention uses 4
Brace StyleAllman (next line)K&R (same line)AllmanAllman is C# community standard
Max Blank Lines12 / 01Collapses consecutive empty lines
Trailing WhitespaceTrimKeepTrimAlways trim for clean diffs
Keyword Spacingif (x)if(x)SpacedApplies to if, for, while, switch, catch
Operator Spacingx = 1x=1SpacedAssignment, comparison, arithmetic
Comma Spacinga, ba,bSpacedSpace after comma only
Semicolon NewlineYesNoYesFor-loop headers excluded
Empty Body Style{ }{}SpacedFor empty methods/classes
Preprocessor IndentNone (col 0)Match scopeNoneStandard is column 0
Using SortAlphabeticalOriginalOriginalSystem namespaces first when sorted
File Size Limit500 KB - Client-side memory constraint
Line EndingLFCRLFLFUnix-style recommended for Git
Final NewlineInsertSkipInsertPOSIX compliance

Frequently Asked Questions

Yes. The tokenizer detects the @" prefix for verbatim strings and $" for interpolated strings. Content inside these literals is treated as opaque and never reformatted. Nested braces inside interpolated expressions are tracked with a depth counter to avoid premature string termination.
This tool performs lexical formatting, not semantic refactoring. It does not build a full abstract syntax tree (AST). Reordering members requires type resolution and dependency analysis that exceeds what a tokenizer can provide. For member reordering, use an IDE plugin backed by Roslyn.
The formatter processes tokens sequentially and does not validate syntax. Mismatched braces will cause indentation to drift (depth d may go negative, clamped to 0). The output will still be produced, but indentation accuracy degrades proportionally to the number of unmatched delimiters.
When Allman style is selected, every opening brace { is placed on a new line at the current indentation depth. In K&R mode, the brace is appended to the preceding statement line after a single space. The formatter tracks whether the preceding token is a closing parenthesis, keyword, or identifier to determine correct placement. Switch-case labels and lambda expressions receive context-aware handling.
The practical limit is approximately 500 KB of source text. Beyond this, the synchronous single-thread tokenizer may cause UI lag exceeding 200 ms. For very large files, consider splitting into partial classes or using a CLI formatter like dotnet format.
Yes. XML doc comments (///) are treated as single-line comments and indented to match their containing scope. Block comments (/* */) preserve internal formatting. Preprocessor directives including #region, #endregion, #if, #else, and #endif are placed at column 0 by default, per standard C# convention.