Used to define and maintain consistent code styles between team members working on the same code as well as between different editors and IDEs that they might use.
All you have to do is drop it into the root of your project. Then any time you open a file in Visual Studio, the .editorconfig file settings will be used to help format the document and also raise warnings if your code style and formatting does not conform. For Visual Studio Code, you can install the EditorConfig for VS Code extension to get support.
Extensive code style settings for C# and VB.NET have been defined that require the latest C# features to be used. All C# related code styles are consistent with StyleCop's default styles, with the exception of the file header definition, which has been changed to a more modern format. All .NET naming conventions are consistent with the .NET Framework Design Guideline's Naming Guidelines.
csharp_space_between_method_call_empty_parameter_list_parentheses
csharp_space_between_method_call_name_and_opening_parenthesis
csharp_space_between_method_declaration_empty_parameter_list_parentheses
csharp_space_between_method_declaration_name_and_open_parenthesis
csharp_space_between_method_declaration_parameter_list_parentheses
dotnet_diagnostic.*
— supported for all compiler warnings.dotnet_naming_rule.*
,dotnet_naming_style.*
,dotnet_naming_symbols.*
— will work if the specified kinds of symbols can be matched by kinds of symbols available in JetBrains Fleet naming settings.
Custom EditorConfig properties that allow configuring code style and code inspection settings. Properties for code inspections can be found here:
Properties for code style and formatting settings are listed in the child pages of the Reformat C# code topic:
Download the .editorconfig
file in this repository and place .editorconfig
file in you current directory.
To help enable an easy workflow and reduce the number of formatting changes requested, this document provides steps to download and enable the various different tools in different development environments to enable a seamless workflow for ensuring that keeping code formatted is a pleasant experience.
C# and VB code in the repository use the built-in Roslyn support for EditorConfig to enable auto-formatting in many IDEs. As a result, no additional tools are required to enable keeping your code formatted. If you want to use dotnet format
to do formatting or are using the git pre-commit hook mentioned later in this document, you can run ./dotnet.cmd tool restore
or ./dotnet.sh tool restore
from the root of the repository to enable the dotnet format
command.
To make the formatting workflow more seamless for contributors, instructions are included below to help enable "format-on-save" or other forms of automatic formatting in various different IDEs or as a Git pre-commit hook for IDEs that do not support "format-on-save" scenarios.
This section is open to contributions for instructions on how to enable "format-on-save"-like semantics in IDEs and editors not mentioned.
Enabling a "format-on-save" experience in VSCode is quite easy. Add the following setting to your .vscode/settings.json
file to enable "format-on-save":
"editor.formatOnSave": true,
The sections below include any additional instructions to configure the experience for different languages.
There are currently some limitations in OmniSharp that causes issues with the auto-formatting. Once a VSCode C# extension releases with both OmniSharp/omnisharp-roslyn#2227 and dotnet/vscode-csharp#4738, then format-on-save should be possible to enable in VSCode for C# and Visual Basic sources.
Visual Studio does not have a "format-on-save" feature but it does have settings for "format on end of statement" or "format on end of block" that can provide some auto-formatting features.
Using these features in combination with the steps specified in the Git Hooks section will enable a seamless formatting experience.
The settings for C#, VB, C, and C++ are all in a dialog under the Tools > Options
menu item.
In the options view, go to Text Editor > C# > Code Style > Formatting > General
for C# settings. Make sure to check the various options for whenever you want Visual Studio to auto-format your code.
Git provides a number of hooks to enable running scripts before commit, push, pull, etc. This section describes adding a pre-commit hook to automatically format code before committing to make formatting seamless even when your development environment doesn't support "format-on-save" or similar functionality with the formatting tools this repository uses.
To enable auto-formatting before committing, you can create a .git/hooks/pre-commit
file in your local dotnet/runtime
clone and add a call to the script located at eng/formatting/format.sh
to auto-format your code before committing. Since Git for Windows also installs Git Bash, this script will work for both Windows and non-Windows platforms.
The following code block can be used as the contents of the pre-commit
file to enable the auto-formatting hook:
#!/bin/sh
./eng/formatting/format.sh