1
1
---
2
2
title : Code style language rules
3
3
description : Learn about the different code style rules for using C# and Visual Basic language constructs.
4
- ms.date : 09/25/2020
4
+ ms.date : 06/22/2022
5
5
ms.topic : reference
6
6
author : gewarren
7
7
ms.author : gewarren
@@ -17,15 +17,15 @@ helpviewer_keywords:
17
17
18
18
Code-style language rules affect how various constructs of .NET programming languages, for example, modifiers, and parentheses, are used. The rules fall into the following categories:
19
19
20
- - [ .NET style rules] ( #net-style-rules ) : Rules that apply to both C# and Visual Basic. The EditorConfig option names for these rules start with ` dotnet_style_ ` prefix .
21
- - [ C# style rules] ( #c-style-rules ) : Rules that are specific to C# language only . The EditorConfig option names for these rules start with ` csharp_style_ ` prefix .
22
- - [ Visual Basic style rules] ( #visual-basic-style-rules ) : Rules that are specific to Visual Basic language only . The EditorConfig option names for these rules start with ` visual_basic_style_ ` prefix .
20
+ - [ .NET style rules] ( #net-style-rules ) : Rules that apply to both C# and Visual Basic. The option names for these rules start with the prefix ` dotnet_style_ ` .
21
+ - [ C# style rules] ( #c-style-rules ) : Rules that are specific to C# code . The option names for these rules start with the prefix ` csharp_style_ ` .
22
+ - [ Visual Basic style rules] ( #visual-basic-style-rules ) : Rules that are specific to Visual Basic code . The option names for these rules start with the prefix ` visual_basic_style_ ` .
23
23
24
24
## Option format
25
25
26
26
Options for language rules can be specified in a [ configuration file] ( ../configuration-files.md ) with the following format:
27
27
28
- ` option_name = value ` (Visual Studio 2019 version 16.9 Preview 2 and later)
28
+ ` option_name = value ` (Visual Studio 2019 version 16.9 and later)
29
29
30
30
or
31
31
50
50
The style rules in this section are applicable to both C# and Visual Basic.
51
51
52
52
- [ 'this.' and 'Me.' qualifiers] ( ide0003-ide0009.md )
53
- - [ dotnet_style_qualification_for_field] ( ide0003-ide0009.md#dotnet_style_qualification_for_field )
54
- - [ dotnet_style_qualification_for_property] ( ide0003-ide0009.md#dotnet_style_qualification_for_property )
55
- - [ dotnet_style_qualification_for_method] ( ide0003-ide0009.md#dotnet_style_qualification_for_method )
56
- - [ dotnet_style_qualification_for_event] ( ide0003-ide0009.md#dotnet_style_qualification_for_event )
57
53
- [ Language keywords instead of framework type names for type references] ( ide0049.md )
58
- - [ dotnet_style_predefined_type_for_locals_parameters_members] ( ide0049.md#dotnet_style_predefined_type_for_locals_parameters_members )
59
- - [ dotnet_style_predefined_type_for_member_access] ( ide0049.md#dotnet_style_predefined_type_for_member_access )
60
54
- [ Modifier preferences] ( modifier-preferences.md#net-modifier-preferences )
61
- - [ csharp_preferred_modifier_order] ( ide0036.md#csharp_preferred_modifier_order )
62
- - [ visual_basic_preferred_modifier_order] ( ide0036.md#visual_basic_preferred_modifier_order )
63
- - [ dotnet_style_require_accessibility_modifiers] ( ide0040.md#dotnet_style_require_accessibility_modifiers )
64
- - [ dotnet_style_readonly_field] ( ide0044.md#dotnet_style_readonly_field )
55
+ - [ Order modifiers (IDE0036)] ( ide0036.md )
56
+ - [ Add accessibility modifiers (IDE0040)] ( ide0040.md )
57
+ - [ Add readonly modifier (IDE0044)] ( ide0044.md )
65
58
- [ Parentheses preferences] ( ide0047-ide0048.md )
66
- - [ dotnet_style_parentheses_in_arithmetic_binary_operators] ( ide0047-ide0048.md#dotnet_style_parentheses_in_arithmetic_binary_operators )
67
- - [ dotnet_style_parentheses_in_relational_binary_operators] ( ide0047-ide0048.md#dotnet_style_parentheses_in_relational_binary_operators )
68
- - [ dotnet_style_parentheses_in_other_binary_operators] ( ide0047-ide0048.md#dotnet_style_parentheses_in_other_binary_operators )
69
- - [ dotnet_style_parentheses_in_other_operators] ( ide0047-ide0048.md#dotnet_style_parentheses_in_other_operators )
70
59
- [ Expression-level preferences] ( expression-level-preferences.md#net-expression-level-preferences )
71
- - [ dotnet_style_object_initializer] ( ide0017.md#dotnet_style_object_initializer )
72
- - [ dotnet_style_collection_initializer] ( ide0028.md#dotnet_style_collection_initializer )
73
- - [ dotnet_style_prefer_auto_properties] ( ide0032.md#dotnet_style_prefer_auto_properties )
74
- - [ dotnet_style_explicit_tuple_names] ( ide0033.md#dotnet_style_explicit_tuple_names )
75
- - [ dotnet_style_prefer_inferred_tuple_names] ( ide0037.md#dotnet_style_prefer_inferred_tuple_names )
76
- - [ dotnet_style_prefer_inferred_anonymous_type_member_names] ( ide0037.md#dotnet_style_prefer_inferred_anonymous_type_member_names )
77
- - [ dotnet_style_prefer_conditional_expression_over_assignment] ( ide0045.md#dotnet_style_prefer_conditional_expression_over_assignment )
78
- - [ dotnet_style_prefer_conditional_expression_over_return] ( ide0046.md#dotnet_style_prefer_conditional_expression_over_return )
79
- - [ dotnet_style_prefer_compound_assignment] ( ide0054-ide0074.md#dotnet_style_prefer_compound_assignment )
80
- - [ dotnet_style_prefer_simplified_interpolation] ( ide0071.md#dotnet_style_prefer_simplified_interpolation )
81
- - [ dotnet_style_prefer_simplified_boolean_expressions] ( ide0075.md#dotnet_style_prefer_simplified_boolean_expressions )
82
- - [ Add missing cases to switch statement] ( ide0010.md ) - This rule has no code style option.
83
- - [ Convert anonymous type to tuple] ( ide0050.md ) - This rule has no code style option.
84
- - [ Use 'System.HashCode.Combine'] ( ide0070.md ) - This rule has no code style option.
85
- - [ Convert 'typeof' to 'nameof'] ( ide0082.md ) - This rule has no code style option.
60
+ - [ Use object initializers (IDE0017)] ( ide0017.md )
61
+ - [ Use collection initializers (IDE0028)] ( ide0028.md )
62
+ - [ Use auto-implemented property (IDE0032)] ( ide0032.md )
63
+ - [ Use explicitly provided tuple name (IDE0033)] ( ide0033.md )
64
+ - [ Use inferred member names (IDE0037)] ( ide0037.md )
65
+ - [ Use conditional expression for assignment (IDE0045)] ( ide0045.md )
66
+ - [ Use conditional expression for return (IDE0046)] ( ide0046.md )
67
+ - [ Use compound assignment (IDE0054 and IDE0074)] ( ide0054-ide0074.md )
68
+ - [ Simplify interpolation (IDE0071)] ( ide0071.md )
69
+ - [ Simplify conditional expression (IDE0075)] ( ide0075.md )
70
+ - [ Add missing cases to switch statement (IDE0010)] ( ide0010.md )
71
+ - [ Convert anonymous type to tuple (IDE0050)] ( ide0050.md )
72
+ - [ Use 'System.HashCode.Combine' (IDE0070)] ( ide0070.md )
73
+ - [ Convert ` typeof ` to ` nameof ` (IDE0082)] ( ide0082.md )
86
74
- [ Null-checking preferences] ( null-checking-preferences.md#net-null-checking-preferences )
87
- - [ dotnet_style_coalesce_expression ] ( ide0029-ide0030.md#dotnet_style_coalesce_expression )
88
- - [ dotnet_style_null_propagation ] ( ide0031.md#dotnet_style_null_propagation )
89
- - [ dotnet_style_prefer_is_null_check_over_reference_equality_method ] ( ide0041.md#dotnet_style_prefer_is_null_check_over_reference_equality_method )
75
+ - [ Use coalesce expression (IDE0029 and IDE0030) ] ( ide0029-ide0030.md )
76
+ - [ Use null propagation (IDE0031) ] ( ide0031.md )
77
+ - [ Use 'is null' check (IDE0041) ] ( ide0041.md )
90
78
- [ File header preferences] ( ide0073.md )
91
- - [ file_header_template] ( ide0073.md#file_header_template )
92
79
93
80
## C# style rules
94
81
95
82
The style rules in this section are applicable to C# language only.
96
83
97
84
- [ 'var' preferences] ( ide0007-ide0008.md )
98
- - [ csharp_style_var_for_built_in_types] ( ide0007-ide0008.md#csharp_style_var_for_built_in_types )
99
- - [ csharp_style_var_when_type_is_apparent] ( ide0007-ide0008.md#csharp_style_var_when_type_is_apparent )
100
- - [ csharp_style_var_elsewhere] ( ide0007-ide0008.md#csharp_style_var_elsewhere )
101
85
- [ Expression-bodied members] ( expression-bodied-members.md )
102
- - [ csharp_style_expression_bodied_constructors ] ( ide0021.md#csharp_style_expression_bodied_constructors )
103
- - [ csharp_style_expression_bodied_methods ] ( ide0022.md#csharp_style_expression_bodied_methods )
104
- - [ csharp_style_expression_bodied_operators ] ( ide0023-ide0024.md#csharp_style_expression_bodied_operators )
105
- - [ csharp_style_expression_bodied_properties ] ( ide0025.md#csharp_style_expression_bodied_properties )
106
- - [ csharp_style_expression_bodied_indexers ] ( ide0026.md#csharp_style_expression_bodied_indexers )
107
- - [ csharp_style_expression_bodied_accessors ] ( ide0027.md#csharp_style_expression_bodied_accessors )
108
- - [ csharp_style_expression_bodied_lambdas ] ( ide0053.md#csharp_style_expression_bodied_lambdas )
109
- - [ csharp_style_expression_bodied_local_functions ] ( ide0061.md#csharp_style_expression_bodied_local_functions )
86
+ - [ Use expression body for constructors (IDE0021) ] ( ide0021.md )
87
+ - [ Use expression body for methods (IDE0022) ] ( ide0022.md )
88
+ - [ Use expression body for operators (IDE0023 and IDE0024) ] ( ide0023-ide0024.md )
89
+ - [ Use expression body for properties (IDE0025) ] ( ide0025.md )
90
+ - [ Use expression body for indexers (IDE0026) ] ( ide0026.md )
91
+ - [ Use expression body for accessors (IDE0027) ] ( ide0027.md )
92
+ - [ Use expression body for lambdas (IDE0053) ] ( ide0053.md )
93
+ - [ Use expression body for local functions (IDE0061) ] ( ide0061.md )
110
94
- [ Pattern matching preferences] ( pattern-matching-preferences.md )
111
- - [ csharp_style_pattern_matching_over_as_with_null_check ] ( ide0019.md#csharp_style_pattern_matching_over_as_with_null_check )
112
- - [ csharp_style_pattern_matching_over_is_with_cast_check ] ( ide0020-ide0038.md#csharp_style_pattern_matching_over_is_with_cast_check )
113
- - [ csharp_style_prefer_switch_expression ] ( ide0066.md#csharp_style_prefer_switch_expression )
114
- - [ csharp_style_prefer_pattern_matching ] ( ide0078.md#csharp_style_prefer_pattern_matching )
115
- - [ csharp_style_prefer_not_pattern ] ( ide0083.md#csharp_style_prefer_not_pattern )
95
+ - [ Use pattern matching to avoid 'as' followed by a 'null' check (IDE0019) ] ( ide0019.md )
96
+ - [ Use pattern matching to avoid 'is' check followed by a cast (IDE0020 and IDE0038) ] ( ide0020-ide0038.md )
97
+ - [ Use switch expression (IDE0066) ] ( ide0066.md )
98
+ - [ Use pattern matching (IDE0078) ] ( ide0078.md )
99
+ - [ Use pattern matching ( ` not ` operator) (IDE0083) ] ( ide0083.md )
116
100
- [ Expression-level preferences] ( expression-level-preferences.md#c-expression-level-preferences )
117
- - [ csharp_style_inlined_variable_declaration ] ( ide0018.md#csharp_style_inlined_variable_declaration )
118
- - [ csharp_prefer_simple_default_expression ] ( ide0034.md#csharp_prefer_simple_default_expression )
119
- - [ csharp_style_pattern_local_over_anonymous_function ] ( ide0039.md#csharp_style_pattern_local_over_anonymous_function )
120
- - [ csharp_style_deconstructed_variable_declaration ] ( ide0042.md#csharp_style_deconstructed_variable_declaration )
121
- - [ csharp_style_prefer_index_operator ] ( ide0056.md#csharp_style_prefer_index_operator )
122
- - [ csharp_style_prefer_range_operator ] ( ide0057.md#csharp_style_prefer_range_operator )
123
- - [ csharp_style_implicit_object_creation_when_type_is_apparent ] ( ide0090.md#csharp_style_implicit_object_creation_when_type_is_apparent )
124
- - [ Add missing cases to switch expression] ( ide0072.md ) - This rule has no code style option.
101
+ - [ Inline variable declaration (IDE0018) ] ( ide0018.md )
102
+ - [ Simplify 'default' expression (IDE0034) ] ( ide0034.md )
103
+ - [ Use local function instead of lambda (IDE0039) ] ( ide0039.md )
104
+ - [ Deconstruct variable declaration (IDE0042) ] ( ide0042.md )
105
+ - [ Use index operator (IDE0056) ] ( ide0056.md )
106
+ - [ Use range operator (IDE0057) ] ( ide0057.md )
107
+ - [ Simplify ` new ` expression (IDE0090) ] ( ide0090.md )
108
+ - [ Add missing cases to switch expression (IDE0072) ] ( ide0072.md )
125
109
- [ "Null" checking preferences] ( null-checking-preferences.md#c-null-checking-preferences )
126
- - [ csharp_style_throw_expression ] ( ide0016.md#csharp_style_throw_expression )
127
- - [ csharp_style_conditional_delegate_call ] ( ide1005.md#csharp_style_conditional_delegate_call )
110
+ - [ Use throw expression (IDE0016) ] ( ide0016.md )
111
+ - [ Use conditional delegate call (IDE1005) ] ( ide1005.md )
128
112
- [ Code block preferences] ( code-block-preferences.md )
129
- - [ csharp_prefer_braces ] ( ide0011.md#csharp_prefer_braces )
130
- - [ csharp_prefer_simple_using_statement ] ( ide0063.md#csharp_prefer_simple_using_statement )
113
+ - [ Add braces (IDE0011) ] ( ide0011.md )
114
+ - [ Use simple 'using' statement (IDE0063) ] ( ide0063.md )
131
115
- [ 'using' directive preferences] ( ide0065.md )
132
- - [ csharp_using_directive_placement] ( ide0065.md#csharp_using_directive_placement )
133
116
- [ Modifier preferences] ( modifier-preferences.md#c-modifier-preferences )
134
- - [ csharp_prefer_static_local_function ] ( ide0062.md#csharp_prefer_static_local_function )
135
- - [ Make struct fields writable] ( ide0064.md ) - This rule has no code style option.
117
+ - [ Make local function static (IDE0062) ] ( ide0062.md )
118
+ - [ Make struct fields writable (IDE0064) ] ( ide0064.md )
136
119
137
120
## Visual Basic style rules
138
121
139
122
The style rules in this section are applicable to Visual Basic language only.
140
123
141
124
- [ Pattern matching preferences] ( pattern-matching-preferences.md )
142
- - [ visual_basic_style_prefer_isnot_expression ] ( ide0084.md#visual_basic_style_prefer_isnot_expression )
125
+ - [ Use pattern matching ( ` IsNot ` operator) (IDE0084) ] ( ide0084.md )
143
126
144
127
## See also
145
128
0 commit comments