Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Code-style rule options

  • 2024-10-08
Feedback

In this article

You can define and maintain consistentcode style in your codebase by defining .NET code-style rules and their associated options in aconfiguration file. These rules are surfaced by various development IDEs, such as Visual Studio, as you edit your code. For .NET projects, these rules can also beenforced at build time. You can enable or disable individual rules and configure the degree to which you want each rule enforced, via a severity level.

Tip

  • When you define code style options in an EditorConfig file, you're configuring how you want thecode style analyzers to analyze your code. The EditorConfig file is the configuration file for these analyzers.
  • Code style options can also be set in Visual Studio in theText editor options dialog. These are per-user options that are only respected while editing in Visual Studio. These options are not respected at build time or by other IDEs. Additionally, if the project or solution opened inside Visual Studio has an EditorConfig file, then options from the EditorConfig file take precedence. In Visual Studio on Windows, you can also generate an EditorConfig file from your text-editor options. SelectTools >Options >Text Editor > [C# orBasic] >Code Style >General, and then clickGenerate .editorconfig file from settings. For more information, seeCode style preferences.

Code style rules are divided into following subcategories:

Each of these subcategories defines its own syntax for specifying options. For more information about these rules and the corresponding options, seeCode-style rule reference.

Example EditorConfig file

To help you get started, here's an example.editorconfig file with the default options.

Note

If you're using .NET 8 or an earlier version, the syntax that's used to specify severity in this example file might not be respected at build time. In that case, you can set the severity by using the rule ID-based severity configuration syntax for analyzers instead. For more information, seeOption format.

Tip

  • In Visual Studio, you can add the following default .NET .editorconfig file to your project from theAdd New Item dialog box. For detailed steps, seeAdd and remove EditorConfig files.
  • For Visual Studio Code, you can use theEditorConfig for VS Code plugin to override user/workspace settings with settings found in .editorconfig files.
# Remove the line below if you want to inherit .editorconfig settings from higher directoriesroot = true# C# files[*.cs]#### Core EditorConfig Options ##### Indentation and spacingindent_size = 4indent_style = spacetab_width = 4# New line preferencesend_of_line = crlfinsert_final_newline = false#### .NET Coding Conventions ##### Organize usingsdotnet_separate_import_directive_groups = falsedotnet_sort_system_directives_first = falsefile_header_template = unset# this. and Me. preferencesdotnet_style_qualification_for_event = falsedotnet_style_qualification_for_field = falsedotnet_style_qualification_for_method = falsedotnet_style_qualification_for_property = false# Language keywords vs BCL types preferencesdotnet_style_predefined_type_for_locals_parameters_members = truedotnet_style_predefined_type_for_member_access = true# Parentheses preferencesdotnet_style_parentheses_in_arithmetic_binary_operators = always_for_claritydotnet_style_parentheses_in_other_binary_operators = always_for_claritydotnet_style_parentheses_in_other_operators = never_if_unnecessarydotnet_style_parentheses_in_relational_binary_operators = always_for_clarity# Modifier preferencesdotnet_style_require_accessibility_modifiers = for_non_interface_members# Expression-level preferencesdotnet_style_coalesce_expression = truedotnet_style_collection_initializer = truedotnet_style_explicit_tuple_names = truedotnet_style_namespace_match_folder = truedotnet_style_null_propagation = truedotnet_style_object_initializer = truedotnet_style_operator_placement_when_wrapping = beginning_of_linedotnet_style_prefer_auto_properties = truedotnet_style_prefer_collection_expression = when_types_loosely_matchdotnet_style_prefer_compound_assignment = truedotnet_style_prefer_conditional_expression_over_assignment = truedotnet_style_prefer_conditional_expression_over_return = truedotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typeddotnet_style_prefer_inferred_anonymous_type_member_names = truedotnet_style_prefer_inferred_tuple_names = truedotnet_style_prefer_is_null_check_over_reference_equality_method = truedotnet_style_prefer_simplified_boolean_expressions = truedotnet_style_prefer_simplified_interpolation = true# Field preferencesdotnet_style_readonly_field = true# Parameter preferencesdotnet_code_quality_unused_parameters = all:silent# Suppression preferencesdotnet_remove_unnecessary_suppression_exclusions = none# New line preferencesdotnet_style_allow_multiple_blank_lines_experimental = truedotnet_style_allow_statement_immediately_after_block_experimental = true#### C# Coding Conventions ##### var preferencescsharp_style_var_elsewhere = falsecsharp_style_var_for_built_in_types = falsecsharp_style_var_when_type_is_apparent = false# Expression-bodied memberscsharp_style_expression_bodied_accessors = truecsharp_style_expression_bodied_constructors = falsecsharp_style_expression_bodied_indexers = truecsharp_style_expression_bodied_lambdas = truecsharp_style_expression_bodied_local_functions = falsecsharp_style_expression_bodied_methods = falsecsharp_style_expression_bodied_operators = falsecsharp_style_expression_bodied_properties = true# Pattern matching preferencescsharp_style_pattern_matching_over_as_with_null_check = truecsharp_style_pattern_matching_over_is_with_cast_check = truecsharp_style_prefer_extended_property_pattern = truecsharp_style_prefer_not_pattern = truecsharp_style_prefer_pattern_matching = truecsharp_style_prefer_switch_expression = true# Null-checking preferencescsharp_style_conditional_delegate_call = true# Modifier preferencescsharp_prefer_static_local_function = truecsharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,asynccsharp_style_prefer_readonly_struct = truecsharp_style_prefer_readonly_struct_member = true# Code-block preferencescsharp_prefer_braces = truecsharp_prefer_simple_using_statement = truecsharp_style_namespace_declarations = block_scopedcsharp_style_prefer_method_group_conversion = truecsharp_style_prefer_primary_constructors = truecsharp_style_prefer_top_level_statements = true# Expression-level preferencescsharp_prefer_simple_default_expression = truecsharp_style_deconstructed_variable_declaration = truecsharp_style_implicit_object_creation_when_type_is_apparent = truecsharp_style_inlined_variable_declaration = truecsharp_style_prefer_index_operator = truecsharp_style_prefer_local_over_anonymous_function = truecsharp_style_prefer_null_check_over_type_check = truecsharp_style_prefer_range_operator = truecsharp_style_prefer_tuple_swap = truecsharp_style_prefer_utf8_string_literals = truecsharp_style_throw_expression = truecsharp_style_unused_value_assignment_preference = discard_variablecsharp_style_unused_value_expression_statement_preference = discard_variable# 'using' directive preferencescsharp_using_directive_placement = outside_namespace# New line preferencescsharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = truecsharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = truecsharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = truecsharp_style_allow_blank_lines_between_consecutive_braces_experimental = truecsharp_style_allow_embedded_statements_on_same_line_experimental = true#### C# Formatting Rules ##### New line preferencescsharp_new_line_before_catch = truecsharp_new_line_before_else = truecsharp_new_line_before_finally = truecsharp_new_line_before_members_in_anonymous_types = truecsharp_new_line_before_members_in_object_initializers = truecsharp_new_line_before_open_brace = allcsharp_new_line_between_query_expression_clauses = true# Indentation preferencescsharp_indent_block_contents = truecsharp_indent_braces = falsecsharp_indent_case_contents = truecsharp_indent_case_contents_when_block = truecsharp_indent_labels = one_less_than_currentcsharp_indent_switch_labels = true# Space preferencescsharp_space_after_cast = falsecsharp_space_after_colon_in_inheritance_clause = truecsharp_space_after_comma = truecsharp_space_after_dot = falsecsharp_space_after_keywords_in_control_flow_statements = truecsharp_space_after_semicolon_in_for_statement = truecsharp_space_around_binary_operators = before_and_aftercsharp_space_around_declaration_statements = falsecsharp_space_before_colon_in_inheritance_clause = truecsharp_space_before_comma = falsecsharp_space_before_dot = falsecsharp_space_before_open_square_brackets = falsecsharp_space_before_semicolon_in_for_statement = falsecsharp_space_between_empty_square_brackets = falsecsharp_space_between_method_call_empty_parameter_list_parentheses = falsecsharp_space_between_method_call_name_and_opening_parenthesis = falsecsharp_space_between_method_call_parameter_list_parentheses = falsecsharp_space_between_method_declaration_empty_parameter_list_parentheses = falsecsharp_space_between_method_declaration_name_and_open_parenthesis = falsecsharp_space_between_method_declaration_parameter_list_parentheses = falsecsharp_space_between_parentheses = falsecsharp_space_between_square_brackets = false# Wrapping preferencescsharp_preserve_single_line_blocks = truecsharp_preserve_single_line_statements = true#### Naming styles ##### Naming rulesdotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestiondotnet_naming_rule.interface_should_be_begins_with_i.symbols = interfacedotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_idotnet_naming_rule.types_should_be_pascal_case.severity = suggestiondotnet_naming_rule.types_should_be_pascal_case.symbols = typesdotnet_naming_rule.types_should_be_pascal_case.style = pascal_casedotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestiondotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_membersdotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case# Symbol specificationsdotnet_naming_symbols.interface.applicable_kinds = interfacedotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protecteddotnet_naming_symbols.interface.required_modifiers = dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enumdotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protecteddotnet_naming_symbols.types.required_modifiers = dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, methoddotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protecteddotnet_naming_symbols.non_field_members.required_modifiers = # Naming stylesdotnet_naming_style.pascal_case.required_prefix = dotnet_naming_style.pascal_case.required_suffix = dotnet_naming_style.pascal_case.word_separator = dotnet_naming_style.pascal_case.capitalization = pascal_casedotnet_naming_style.begins_with_i.required_prefix = Idotnet_naming_style.begins_with_i.required_suffix = dotnet_naming_style.begins_with_i.word_separator = dotnet_naming_style.begins_with_i.capitalization = pascal_case

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo