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
ifandforeach. - 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/awaitandusingstatements. - 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 DEBUGand#region/#endregionare 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.