FormatterHQ

C# Code Formatter

Format C# classes, records, LINQ queries and properties. The Microsoft preset matches common .NET conventions: Allman braces and 4-space indentation.

Input CSHARP
Output CSHARP
Ready.

How to use the C# Formatter

  1. Paste your C# source code into the Input editor, or use Sample, Upload file or Load URL.
  2. Pick a Style preset (default: Microsoft), keep Indent on Style default or override it, and set the Column limit (0 means no limit).
  3. Press Format; with Auto update on, the output refreshes as you edit.
  4. Use Copy or Download above the output panel to take the formatted file with you.

A C# formatter for .NET code

This C# formatter uses clang-format's C# mode, running as WebAssembly in your browser. It lays out classes, records, properties, lambdas, LINQ chains and attributes according to a style preset. The default Microsoft preset is closest to the conventions used across the .NET ecosystem: every brace on its own line (Allman style) and 4-space indentation.

Example

public class OrderService{private readonly IRepo _repo;public OrderService(IRepo repo)=>_repo=repo;public async Task<IEnumerable<Order>> RecentAsync(int days){var since=DateTime.UtcNow.AddDays(-days);return (await _repo.AllAsync()).Where(o=>o.Created>since).OrderByDescending(o=>o.Created);}public string Name{get;set;}}
public class OrderService
{
    private readonly IRepo _repo;
    public OrderService(IRepo repo) => _repo = repo;
    public async Task<IEnumerable<Order>> RecentAsync(int days)
    {
        var since = DateTime.UtcNow.AddDays(-days);
        return (await _repo.AllAsync())
            .Where(o => o.Created > since)
            .OrderByDescending(o => o.Created);
    }
    public string Name { get; set; }
}

The expression-bodied constructor stays on one line, the LINQ chain wraps before each dot, and the auto-property is printed as { get; set; } with spaces inside the braces.

C# conventions the Microsoft preset follows

  • Allman braces for namespaces, types, methods and control statements, as in the .NET runtime and most Microsoft documentation samples.
  • 4 spaces per indent level, no tabs.
  • Spaces around binary operators, after commas and after keywords like if and foreach.
  • 120-column guideline in the preset; this tool starts at 100, which you can raise.

One difference from Visual Studio defaults: with a block-scoped namespace X { }, the Microsoft preset does not indent the types inside the namespace. File-scoped namespaces (namespace X;), common since C# 10, avoid the question entirely.

Other presets

If your team prefers K&R braces (opening brace at the end of the line), choose Google, LLVM or Chromium; these produce 2-space indentation unless you set Indent to 4. Unity projects often use Allman braces as well, so the Microsoft preset works for most game scripts.

What is formatted

  • Classes, structs, interfaces, records (public record Order(int Id, decimal Total);) and enums.
  • Properties, indexers, events, expression-bodied members and local functions.
  • Lambdas, LINQ method syntax, async/await and using statements.
  • Attributes like [HttpGet("{id}")], generics with constraints, and nullable annotations (string?).
  • Interpolated strings ($"Hello {name}") and verbatim strings are kept verbatim.

Limits of clang-format for C#

clang-format does not parse C# the way the Roslyn compiler does. It will not reorder using directives by System-first rules, apply naming conventions, or add braces to single-line if statements. Very new syntax, such as collection expressions or some pattern-matching forms, may be spaced conservatively. For project-wide enforcement, dotnet format with an .editorconfig is the standard .NET tool; this page is ideal for quickly tidying a snippet, a Stack Overflow answer or generated code.

Common problems

  • Everything after a point is mis-indented: usually an unbalanced brace or an unterminated string. clang-format does not report syntax errors, so the odd indentation is your hint.
  • Preprocessor directives such as #if DEBUG and #region / #endregion are kept at the start of the line, and the code between them is indented normally.
  • XML doc comments (/// <summary>) are kept; long ones are not re-wrapped.

To create C# classes from an API response, try JSON to C#. To escape text for a C# string literal, use .NET Escape. All formatting happens locally in your browser.

Frequently asked questions

Which style matches Visual Studio’s default C# formatting?
The Microsoft preset is the closest: Allman braces and 4-space indentation. Visual Studio indents types inside block-scoped namespaces, which this preset does not, so the result can differ there.
Does it support records and file-scoped namespaces?
Yes. Record declarations, primary constructors on records and file-scoped namespaces are formatted like other type declarations.
Does it sort using directives?
No. Using directives are kept in their original order. Use dotnet format or your IDE to sort and remove unused ones.
Can I use it for Unity scripts?
Yes. Unity C# scripts are ordinary C#, and the Microsoft preset matches the Allman brace style used in Unity’s documentation.
Why is my code not reported as invalid?
clang-format does not compile code. It formats tokens, so compile errors are left for the C# compiler.

Last updated September 26, 2026

Related tools