Code style preferences

You can define code style settings per-project by using an EditorConfig file, or for all code you edit in Visual Studio on the text editor Options page. For C# code, you can also configure Visual Studio to apply these code style preferences using the Code Cleanup (Visual Studio 2019, Visual Studio 2022) and Format Document (Visual Studio 2017) commands.

Note

This topic applies to Visual Studio on Windows. For Visual Studio for Mac, see Editor behavior in Visual Studio for Mac.

Code styles in EditorConfig files

Code style settings for .NET can be specified by adding an EditorConfig file to your project. EditorConfig files are 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. Use an EditorConfig file when you want to enforce coding styles for all contributors to your repo or project.

You can manually populate your EditorConfig file, or you can automatically generate the file based on the code style settings you've chosen in the Visual Studio Options dialog box. This options page is available at Tools > Options > Text Editor > [C# or Basic] > Code Style > General. Click Generate .editorconfig file from settings to automatically generate a coding style .editorconfig file based on the settings on this Options page.

Screenshot of Generate editorconfig file from settings.

You can manually populate your EditorConfig file, or you can automatically generate the file based on the code style settings you've chosen in the Visual Studio Options dialog box. This options page is available at Tools > Options > Text Editor > [C# or Visual Basic] > Code Style > General. Click Generate .editorconfig file from settings to automatically generate a coding style .editorconfig file based on the settings on this Options page.

Screenshot of Generate editorconfig file from settings.

Code styles in the Options dialog box

Code style preferences can be set for all of your C# and Visual Basic projects by opening the Options dialog box from the Tools menu. In the Options dialog box, select Text Editor > [C# or Visual Basic] > Code Style > General.

Code style preferences can be set for all of your C# and Visual Basic projects by opening the Options dialog box from the Tools menu. In the Options dialog box, select Text Editor > [C# or Basic] > Code Style > General.

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

Screenshot of code style options.

Screenshot of code style options.

Options set in this window are applicable to your Visual Studio personalization account and aren't associated with a particular project or codebase. In addition, they aren't enforced at build time, including in continuous integration (CI) builds. If you want to associate code style preferences with your project and have the styles enforced during build, specify the preferences in an .editorconfig file that's associated with the project.

Preference and severity

For each code style setting on this page, you can set the Preference and Severity values using the drop-downs on each line. Severity can be set to Refactoring Only, Suggestion, Warning, or Error. If you want to enable Quick Actions for a code style, ensure that the Severity setting is set to something other than Refactoring Only. The Quick Actions light bulb , error light bulb , or screwdriver icon appears when a non-preferred style is used, and you can choose an option on the Quick Actions list to automatically rewrite code to the preferred style.

Enforce code styles on build

Starting in Visual Studio 2019 version 16.8, which includes the .NET 5.0 RC2 SDK, you can enforce the .NET coding conventions on build for all .NET projects. At build time, .NET code style violations will appear as warnings or errors with an "IDE" prefix. This enables you to strictly enforce consistent code styles in your codebase.

Apply code styles

For C# code files, Visual Studio has a Code Cleanup button at the bottom of the editor (keyboard: Ctrl+K, Ctrl+E) to apply code styles from an EditorConfig file or from the Code Style options page. If an .editorconfig file exists for the project, those are the settings that take precedence.

Screenshot of Execute code cleanup.

Tip

Rules configured with a severity of None don't participate in code cleanup but can be individually applied via the Quick Actions and Refactorings menu.

First, configure which code styles you want to apply (in one of two profiles) in the Configure Code Cleanup dialog box. To open this dialog box, click the expander arrow next to the code cleanup broom icon and then choose Configure Code Cleanup.

Screenshot of Configure Code Cleanup.

After you've configured code cleanup, you can either click on the broom icon or press Ctrl+K, Ctrl+E to run code cleanup. You can also run code cleanup across your entire project or solution. Right-click on the project or solution name in Solution Explorer, select Analyze and Code Cleanup, and then select Run Code Cleanup.

Screenshot of Run Code Cleanup across entire project or solution.

For C# code files, Visual Studio has a Code Cleanup button at the bottom of the editor (keyboard: Ctrl+K, Ctrl+E) to apply code styles from an EditorConfig file or from the Code Style options page. If an .editorconfig file exists for the project, those are the settings that take precedence.

Screenshot of Execute code cleanup.

Tip

Rules configured with a severity of None don't participate in code cleanup but can be individually applied via the Quick Actions and Refactorings menu.

First, configure which code styles you want to apply (in one of two profiles) in the Configure Code Cleanup dialog box. To open this dialog box, click the expander arrow next to the code cleanup broom icon and then choose Configure Code Cleanup.

Screenshot of Configure Code Cleanup.

After you've configured code cleanup, you can either click on the broom icon or press Ctrl+K, Ctrl+E to run code cleanup. You can also run code cleanup across your entire project or solution. Right-click on the project or solution name in Solution Explorer, select Analyze and Code Cleanup, and then select Run Code Cleanup.

Screenshot of Run Code Cleanup across entire project or solution.

Code cleanup settings

Most of the code cleanup settings map to one or more .NET code styles supported in .editorconfig. For examples that show the effects of the settings, use the links in the following table.

Setting Rule ID or style option
Remove unnecessary Imports or usings IDE0005
Apply file header preferences file_header_template
Remove unused variables IDE0051
Apply object creation preferences visual_basic_style_prefer_simplified_object_creation
Apply IsNot preferences visual_basic_style_prefer_isnot_expression
Add 'this' or 'Me' qualification IDE0003-IDE0009
Add accessibility modifiers dotnet_style_require_accessibility_modifiers
Order modifiers IDE0036
Make field readonly dotnet_style_readonly_field
Remove unnecessary casts IDE0004
Apply object/collection initialization parameters) dotnet_style_object_initializer
Apply using directive placement preferences csharp_using_directive_placement
Apply parentheses preferences IDE0047-IDE0048
Apply unused value preferences IDE0058
Apply language/framework type preferences IDE0049
Remove unused suppressions dotnet_remove_unnecessary_suppression_exclusions
Apply simplify boolean expression preferences dotnet_style_prefer_simplified_boolean_expressions
Apply string interpolation preferences dotnet_style_prefer_simplified_interpolation
Remove unused parameters dotnet_code_quality_unused_parameters
Apply auto property preferences dotnet_style_prefer_auto_properties
Apply compound assignment preferences dotnet_style_prefer_compound_assignment
Apply coalesce expression preferences dotnet_style_coalesce_expression
Apply conditional expression preferences dotnet_style_prefer_conditional_expression_over_assignment
dotnet_style_prefer_conditional_expression_over_return
Apply tuple name preferences dotnet_style_explicit_tuple_names
dotnet_style_prefer_inferred_tuple_names
Apply inferred anonymous type member names preferences dotnet_style_prefer_inferred_anonymous_type_member_names
Apply null checking preferences dotnet_style_prefer_is_null_check_over_reference_equality_method
Apply null propagation preferences dotnet_style_null_propagation
Apply 'var' preferences IDE0007-IDE0008
Add required braces for single-line control statements csharp_prefer_braces
Apply expression/block body preferences Expression-bodied members
Apply inline 'out' variables preferences csharp_style_inlined_variable_declaration
Apply pattern matching preferences Pattern-matching preferences
Apply conditional delegate call preferences csharp_style_conditional_delegate_call
Apply static local function preferences csharp_prefer_static_local_function
Apply deconstruct preferences csharp_style_deconstructed_variable_declaration
Apply default(T) preferences csharp_prefer_simple_default_expression
Apply new() preferences csharp_style_implicit_object_creation_when_type_is_apparent
Apply range preferences csharp_style_prefer_range_operator
Apply local over anonymous function preferences csharp_style_pattern_local_over_anonymous_function
Apply parameter null preferences Null-checking preferences
Apply using statement preferences csharp_prefer_simple_using_statement
Apply throw expression preferences csharp_style_throw_expression

If you want your code style settings to be applied every time you save a file, go to Options > Text Editor > Code Cleanup and select Run Code Cleanup profile on save.

If you want your code style settings to be applied every time you save a file, you may like the Code Cleanup on Save extension.