Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
For .NET, the code style preferences are based on .NET compiler platform (Roslyn) analyzers. For more information, see Code analysis using .NET compiler platform (Roslyn) analyzers.
In Visual Studio 2019 version 16.5 and later, rule set files are deprecated in favor of EditorConfig files for code style configuration for .NET code. For more information, see Convert an existing rule set file to an EditorConfig file.
Code styles in EditorConfig files
Code style settings 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. They are especially useful to ensure consistency in a teams programming environment.
To add an EditorConfig file, see Add and remove EditorConfig files.
For reference information on .NET code style settings, see Code style settings.
C/C++ code styles in the Options dialog box
For more information about all the formatting options, see Options, Text Editor, C/C++, Formatting.
.NET code styles in the Options dialog box
Each item in the list shows a preview of the preference when you select the option:
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 the General and Naming options pages, 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.
- Code style preferences in the General and Naming options pages determine the Quick Actions for a code style. 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. - If you want the violation to appear only as a Quick Action and also be excluded from code cleanup, set the Severity to Refactoring Only.
- If you want the violation to appear in the Error List window as well as Quick Actions, and be included in code cleanup, set the Severity to Suggestion, Warning, or Error. Violations appear as a suggestion (message), warning, or error when you set the output in the Error List window to Build + IntelliSense. They are excluded from Build only output.
Settings in an EditorConfig file take precedence over code styles set in these pages.
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
When you change a code style in the Options page or add an EditorConfig file to your project in Visual Studio, only new lines of code are formatted based on the new settings. The formatting of existing code isn't changed unless you run one of the following commands:
-
Select Code Cleanup in the Visual Studio editor, or press Ctrl+K, Ctrl+E. This command applies to white-space settings, such as indent style, and selected code-style settings, such as parenthesis preferences.
Format Document.
Select Edit > Advanced > Format Document, or press Ctrl+K, Ctrl+D in the default profile. This command applies only to white-space settings, such as indent style.
Apply code styles using code cleanup
.NET 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 |
| Sort imports or usings | dotnet_sort_system_directives_first dotnet_separate_import_directive_groups |
| Apply file header preferences | file_header_template |
| Remove unused variables | CS0219 |
| 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 |