EditorConfig for enforcing code style

EditorConfig can be used to define and maintain consistent coding styles in Visual Studio 2017. You don’t need to install any plugins in VS 2017 to use EditorConfig. VS 2017 comes bundled with native support for EditorConfig.

All you need to add “.editorconfig” file into your VS Solution.

For topmost .editorconfig file, you need to set root = true . As shown in sample .editorconfig file shown below:

# EditorConfig is awesome: http://EditorConfig.org # top-most EditorConfig file root = true

You can even override it at individual project level.

For child directories’s .editorconfig files, you need to set root = false . As shown in sample .editorconfig file shown below:

# EditorConfig is awesome: http://EditorConfig.org # child-directory EditorConfig file root = false

Format for defining rules for language conventions in EditorConfig in VS 2017

options_name = false|true : none|suggestion|warning|error

Some examples:

1. dotnet_style_predefined_type_for_locals_parameters_members:

If we have the following rule defined in .editorconfig file:

[*.cs] dotnet_style_predefined_type_for_locals_parameters_members = true : warning

The below code will give “Name can be simplified” warning:

2. csharp_style_pattern_matching_over_is_with_cast_check:

If we have the following rule defined in .editorconfig file:

[*.cs] csharp_style_pattern_matching_over_is_with_cast_check = true : error

The below code will give “Use pattern matching” error:

3. csharp_style_inlined_variable_declaration:

If we have the following rule defined in .editorconfig file:

[*.cs] csharp_style_inlined_variable_declaration = true : error

The below code will give “Variable declaration can be inlined” error:

4. csharp_style_var_for_built_in_types:

If we have the following rule defined in .editorconfig file:

[*.cs] csharp_style_var_for_built_in_types = false : warning

The below code will give “Use explicit type instead of ‘var'” warning:

5. csharp_style_expression_bodied_constructors:

If we have the following rule defined in .editorconfig file:

[*.cs] csharp_style_expression_bodied_constructors = true : error

The below code will give “Use expression body for constructors” error:

Format for defining rules for formatting conventions in EditorConfig in VS 2017

rule_name = false|true

Example:

dotnet_sort_system_directives_first:

Following rule defined in .editorconfig file:

[*.cs] dotnet_sort_system_directives_first = true

Enforcing C# 7’s code style & patterns using EditorConfig in VS 2017

#---------------------------------------------------------------------------------------- # C# 7 #---------------------------------------------------------------------------------------- # 1. Pattern matching [*.cs] csharp_style_pattern_matching_over_is_with_cast_check = true : warning [*.cs] csharp_style_pattern_matching_over_as_with_null_check = true : warning # 2. Inlined variable declarations [*.cs] csharp_style_inlined_variable_declaration = true : warning # 3. Explicit Tuple names [*.cs] dotnet_style_explicit_tuple_names = true : warning # 4. Expression Bodied [*.cs] csharp_style_expression_bodied_constructors = true : warning [*.cs] csharp_style_expression_bodied_operators = true : warning [*.cs] csharp_style_expression_bodied_properties = true : warning [*.cs] csharp_style_expression_bodied_indexers = true : warning [*.cs] csharp_style_expression_bodied_accessors = true : warning # 5. default literal for default value expressions [*.cs] csharp_prefer_simple_default_expression = true : warning # 6. throw expression [*.cs] csharp_style_throw_expression = true : warning #----------------------------------------------------------------------------------------

Final EditorConfig file that I have setup:

# EditorConfig is awesome: http://EditorConfig.org # top-most EditorConfig file root = true # Code style settings [*.cs] dotnet_style_predefined_type_for_locals_parameters_members = true : warning [*.cs] dotnet_style_predefined_type_for_member_access = true : warning [*.cs] dotnet_style_object_initializer = true : warning [*.cs] dotnet_style_coalesce_expression = true : warning [*.cs] dotnet_style_null_propagation = true : warning [*.cs] csharp_style_var_for_built_in_types = false : warning [*.cs] csharp_style_var_when_type_is_apparent = true : warning [*.cs] csharp_style_expression_bodied_methods = true : warning [*.cs] csharp_prefer_braces = true : warning # Formatting [*.cs] dotnet_sort_system_directives_first = true #---------------------------------------------------------------------------------------- # C# 7 #---------------------------------------------------------------------------------------- # 1. Pattern matching [*.cs] csharp_style_pattern_matching_over_is_with_cast_check = true : warning [*.cs] csharp_style_pattern_matching_over_as_with_null_check = true : warning # 2. Inlined variable declarations [*.cs] csharp_style_inlined_variable_declaration = true : warning # 3. Explicit Tuple names [*.cs] dotnet_style_explicit_tuple_names = true : warning # 4. Expression Bodied [*.cs] csharp_style_expression_bodied_constructors = true : warning [*.cs] csharp_style_expression_bodied_operators = true : warning [*.cs] csharp_style_expression_bodied_properties = true : warning [*.cs] csharp_style_expression_bodied_indexers = true : warning [*.cs] csharp_style_expression_bodied_accessors = true : warning # 5. default literal for default value expressions [*.cs] csharp_prefer_simple_default_expression = true : warning # 6. throw expression [*.cs] csharp_style_throw_expression = true : warning #----------------------------------------------------------------------------------------

Happy Coding !!!