C# Formatting the .NET Way: Braces, Usings and .editorconfig Explained
How the default .NET rules lay out braces, using directives and spacing, which .editorconfig keys control them, and how to format C# in a browser.

C# formatting is unusual among languages because its rules live in a plain text file that every major editor reads. Visual Studio, Rider, VS Code with the C# extension and the dotnet format command all take their formatting options from .editorconfig. Once you know the handful of keys that matter, you can make any machine produce the same layout. The C# Code Formatter reads and writes those keys and applies them to files in your browser.
The defaults in one paragraph
Out of the box, .NET puts every opening brace on its own line, a layout often called Allman style. else, catch and finally also start new lines. Blocks are indented by four spaces, switch labels are indented inside the switch, and their statements one level further. Control keywords such as if, for, foreach and while are followed by a space before the parenthesis. using directives are sorted with the System namespaces first.
The keys that control braces
csharp_new_line_before_open_brace takes all, none, or a list of contexts such as types, methods and properties. all is the default. none gives K&R style with braces at the end of the line. A list like types,methods keeps braces on new lines for declarations but attaches them for if and loops, a compromise some teams prefer.
csharp_new_line_before_else, csharp_new_line_before_catch and csharp_new_line_before_finally decide whether those keywords start a new line. With K&R braces you will usually set them to false, so the code reads "} else {".
Sorting using directives
dotnet_sort_system_directives_first puts System and System.* first and sorts the rest alphabetically. dotnet_separate_import_directive_groups adds a blank line between groups that start with a different first name, such as System, Microsoft and your own company. The formatter also places global using lines at the top and keeps using static directives and aliases after the normal ones, and it removes exact duplicates.
Literals the formatter never touches
C# has more string forms than most languages: regular strings with escapes, verbatim strings with doubled quotes, interpolated strings whose holes can contain more strings and ternaries, and raw string literals delimited by three or more quotes. The tokenizer reads each of them as a single unit, so a brace inside an interpolated string or a quote inside a verbatim path cannot confuse the indentation. Comments are copied unchanged as well.
Spacing details that trip up simple tools
A colon has three different jobs in C#. In a class declaration or a constructor initializer it separates a base type and gets a space on each side. In a named argument such as name: value it stays next to the name. In a ternary it gets spaces again. The formatter handles all three. It also tells the nullable type int? and the null-forgiving operator obj! apart from a ternary question mark and a logical not, so they stay attached to the word before them.
Importing and exporting .editorconfig
If your repository already has an .editorconfig, paste it or open it in the .editorconfig section and press Apply to options. Only keys in sections that match .cs files are read, so a [*.md] section cannot change your C# settings by accident. To go the other way, set the options in the form until the diff looks right, then download a fresh .editorconfig and commit it next to your solution file.
Checks while you format
The issues list catches mistakes that are easy to miss in a long file: brackets that do not match, strings that never close, a #region without its #endregion, and async void methods that are not event handlers. The last one matters because exceptions thrown from an async void method cannot be caught by the caller and can crash the process.
What it does not do
The built-in engine works on tokens rather than on a full syntax tree, so it keeps your line breaks. It will not wrap a long argument list, add braces to a one-line if, or reorder class members. Use it to normalise layout and imports quickly, and keep dotnet format in your build to enforce the rules on every commit.



