Enforcing C# 7’s code style & latest coding patterns using EditorConfig in Visual Studio 2017

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.

editorconfig VS 2017 - add to 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.

editorconfig override VS 2017

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:

editorconfig vs 2017 rule dotnet_style_predefined_type_for_locals_parameters_members

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:

editorconfig VS 2017 - use pattern matching - csharp_style_pattern_matching_over_is_with_cast_check rule

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:

editorconfig VS 2017 - variable decalaration can be inlined - csharp_style_inlined_variable_declaration rule

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:

editorconfig VS 2017 - explicit type over var for built-in types - csharp_style_var_for_built_in_types rule

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:

editorconfig VS 2017 - expression bodied constructor - csharp_style_expression_bodied_constructors rule

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 !!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *