Home › Developer Tools › C# Formatter

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.

Advertisement
Advertisement

How to use the C# Formatter

  1. Paste your C# code into the left editor, open a .cs file, or load a sample.
  2. Choose the brace style: Allman (new line, the .NET default), K&R (same line) or Stroustrup.
  3. Set indentation and line width; 4 spaces and 120 columns match common .NET settings.
  4. Check the result on the right, which updates as you type, or press Format C# (Ctrl + Enter).
  5. 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 spacesindent_style = space, indent_size = 44 spaces
Brace style: Allmancsharp_new_line_before_open_brace = allall (Allman)
Brace style: K&Rcsharp_new_line_before_open_brace = none-
else / catch / finally on a new linecsharp_new_line_before_else, _catch, _finally = truetrue
Indented case labelscsharp_indent_switch_labels = truetrue
No space after a castcsharp_space_after_cast = falsefalse
Space after if, for, whilecsharp_space_after_keywords_in_control_flow_statements = truetrue
Line widthmax_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, .Select and 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.

Related tools

Java FormatterBeautify Java code online: indent size, brace style and line width, with clang-format option.JSON to C# ClassConvert JSON to C# classes or records with System.Text.Json or Newtonsoft attributes.Code Diff CheckerCompare two code files side by side or unified, with syntax colors and changes highlighted.SQL Formatter & BeautifierFormat, beautify or minify SQL queries for MySQL, PostgreSQL and more.JSON FormatterBeautify, minify and validate JSON, with a collapsible tree view.JavaScript BeautifierFormat and beautify messy or minified JavaScript, or minify it.HTML FormatterBeautify and indent HTML code with a live preview.CSS FormatterBeautify CSS with indent controls and optional property sorting.XML FormatterBeautify, validate and minify XML with a collapsible tree view.ZIP File CreatorCompress files and folders into a ZIP.YAML Validator & FormatterValidate, format and minify YAML, and convert YAML to JSON or back.HTML MinifierMinify HTML: strip comments and whitespace safely and see how many bytes you save.CSS MinifierMinify CSS with csso or a built-in engine and see the size saved instantly.JavaScript MinifierMinify JavaScript with Terser: compress, mangle and see how many bytes you save.JSON MinifierMinify and validate JSON, sort keys and see exactly how many bytes you save.IPv6 Expander & CompressorExpand IPv6 addresses to full form or compress them per RFC 5952, one or many at once.GraphQL FormatterPretty-print and indent GraphQL queries, mutations and schemas, or minify them, instantly.CSS ValidatorValidate CSS: syntax errors, unknown properties and invalid values by line, plus auto-fix.Gzip Compress / DecompressGzip or deflate text and files, get Base64 output, and decompress Base64 or .gz files.IBAN ValidatorCheck an IBAN's mod-97 checksum, country and length, and see its bank code and account parts.JavaScript Syntax CheckerCheck JavaScript for syntax errors with line and column, for any ES version, script or module.JSON Schema ValidatorValidate JSON against a JSON Schema (draft-07, 2019-09, 2020-12) with clickable error paths.Markdown FormatterBeautify Markdown with Prettier: list markers, prose wrap, aligned tables and live preview.PHP FormatterBeautify PHP code with Prettier PHP: PSR-12 style, indent and brace options, syntax errors.TypeScript FormatterFormat TypeScript and TSX code with Prettier: width, tabs, semicolons and quotes options.XML MinifierMinify and validate XML: strip whitespace and comments, keep CDATA, see the size savings.HTML ValidatorCheck HTML for errors and warnings with line numbers, locally or with the W3C Nu checker.JavaScript ObfuscatorObfuscate JavaScript with string encoding, renaming and control-flow flattening presets.Phone Number FormatterFormat and validate lists of phone numbers to E.164, international or national style.Python FormatterFormat Python code in Black/Ruff style: line length, quote style, indent, copy or download.Responsive Design CheckerPreview any website at phone, tablet, laptop and desktop sizes side by side, with rotate and zoom.Browser Compatibility CheckerSearch CSS, JavaScript and HTML features and see which browser versions support them.Core Web Vitals CheckerCheck real-user LCP, INP and CLS for any URL or site and see if it passes Core Web Vitals.Heading Structure CheckerPaste HTML to see its H1-H6 outline and catch missing H1s, multiple H1s and skipped levels.Website Hosting CheckerFind out who hosts a website: hosting provider, IP addresses, ASN, network owner and location.Routing Number ValidatorCheck a US ABA routing number's checksum and format and see its Federal Reserve district.Spam Word CheckerScan an email subject and body for spam trigger words, excess caps, exclamation marks and risky links.Check Digit CalculatorCalculate or validate check digits for UPC, EAN, GTIN, SSCC, ISBN, ISSN and mod 11 codes.Luhn Algorithm CheckerValidate numbers with the Luhn mod 10 check or calculate the missing check digit, step by step.XSD ValidatorValidate XML against an XSD schema in your browser and see each error with its line number.Strftime Format BuilderBuild Python and C strftime format strings by clicking tokens, with a live preview and reference.
See all 41 Code Formatters & Validators →