Code Formatting

Format and reformat code for consistency in your solution.

Format and Reformat Code

Code that is formatted consistently is more readable, and more enjoyable to work with. But what is good formatting? How to use whitespaces and blank lines to arrange and separate code blocks, should you use tabs for indents (or spaces?), and many more questions typically arise in these discussions.

ReSharper comes with formatting rules that can suit every team. There are default formatting rules based on the default Visual Studio formatting options and numerous best practices. You can configure every detail of formatting rules, and ReSharper will apply it for you, as you type, or when you invoke Code Cleanup.

Formatting rules are applied when ReSharper runs code completion and code generation, and when refactorings are performed. Formatting rules can also be applied to existing code.

Format Selection from Alt+Enter

When you select code in the editor and use the Alt+Enter shortcut, you can format that selected portion of code using the current code formatting configuration. There's also an option to configure formatting rules based on the selected code, so you can see the effect of code formatting rules on your own code and tweak it to your preference.

.editorconfig

EditorConfig is a configuration file convention that is used to define and maintain consistent code styles between team members working on the same code, and between different editors and IDEs. A mixed team of ReSharper and Rider users is definitely possible, and code styles will be consistent across the two!

Style configuration is saved in files named .editorconfig, and can be shared easily by adding it to your Git repository (or other source control).

ReSharper will apply code styles defined in files named .editorconfig in the directory of the current file and in all parent directories.

You can export code styles from ReSharper to .editorconfig. No need to dive into the EditorConfig syntax - use Format Selection and let ReSharper generate this syntax for you.

Tip: When not using .editorconfig, you can also share code styles (and other ReSharper settings) using Settings Layers.

Code Cleanup

Based on the current code style, or using custom profiles, you can run code cleanup in bulk for a project or even a full solution. A command line code formatting tool is also available.

File/Type Layout

Code styles not only apply to the syntax, but also apply to the order of elements in a file or type. You can configure how to reorder type members when cleaning up your code.

See Also