Visual Studio Code Style Preferences

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

You can define and maintain a consistent code style in your codebase by defining .NET code style rule options in an EditorConfig file.

  • Code style settings can be specified by adding an EditorConfig file to your project.
  • The editor configuration file is associated with a codebase rather than a Visual Studio personalization account.
  • Settings in an EditorConfig file take precedence over code styles that are specified in the Options dialog box.
  • It is useful when you want to enforce coding styles for all contributors to your project.

The code style preferences can be set on the Options dialog box from the Tools > Options > Text Editor > [C# or Basic] > Code Style > General.

image
  • All the settings on this dialog apply to your Visual Studio personalization account and aren't associated with a particular project or codebase.
  • These settings aren't enforced at build time, if you want to associate code style preferences with your project and have the styles enforced during the build, specify the preferences in a .editorconfig file that is associated with the project.

Each item in the list shows a preview of the preference when selected.

image

You can specify the Preference and Severity values using the drop-downs on each line for each code style setting.

image The following options are available to set Severity
  • Refactoring Only
  • Suggestion
  • Warning
  • Error

If you want to enable Quick Actions for a code style, make sure that the Severity setting is set to other than the Refactoring Only option.

Generate Coding Style Configuration File

To generate a coding style file, click the Generate .editorconfig file from settings button.

image

It will generate a coding style .editorconfig file based on the settings on the Options page.

Apply Code Styles

In Visual Studio 2019, for C# code files you will see a Code Cleanup button at the bottom of the editor.

image

If a .editorconfig file exists for the project, those are the settings that take precedence.

Configuration

With the Code Cleanup broom icon, you can see an expander arrow, let's click that arrow and you will see the following menu.

image

To configure which code styles you want to apply in one of two profiles in the Configure Code Cleanup dialog box.

image

After you have configured code cleanup, you can either click on the broom icon or press Ctrl+K, Ctrl+E to run code cleanup to apply code styles from an EditorConfig file or the Code Style options page.



Got any Visual Studio Question?