.editorconfig is a file format that defines coding styles and standards across different editors and IDEs. It ensures consistent code formatting across your entire development team, regardless of their chosen development environment.
The .editorconfig file uses a simple INI-style format with sections for different file types:
[*] # All files
[*.cs] # C# specific
[*.{xaml,xml} # Multiple file types
Essential configurations include:
.NET projects benefit from strict naming rules:
# Interface naming
dotnet_naming_rule.interface_should_be_begins_with_i.severity = error
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
# Private fields
dotnet_naming_rule.private_fields_should_be_camel_case.severity = warning
dotnet_naming_rule.private_fields_should_be_camel_case.symbols = private_fields
dotnet_naming_rule.private_fields_should_be_camel_case.style = camel_case_underscore_style
From a production MAUI project:
# Editor configuration, see http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
max_line_length = off
trim_trailing_whitespace = false
From a production backend service:
[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
[*.cs]
# IDE0047: Remove unnecessary parentheses
dotnet_diagnostic.IDE0047.severity = silent
From a production Blazor project:
[*.{razor,cshtml}]
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
# Component-specific rules
[*.razor]
dotnet_style_coalesce_expression = true:warning
csharp_style_expression_bodied_properties = true:warning
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
Different project types benefit from tailored rules:
public class LoginPageModel : BaseViewModel, ISingletonDependency
{
private readonly FilterPanelViewModel _filterPanelViewModel;
[ObservableProperty] private DataGridViewModel _dataGridViewModel;
[ObservableProperty] private bool _filterPanelVisible;
}
Demonstrates proper naming conventions and attribute usage in MAUI.
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
public async Task<IActionResult> GetForecast()
{
// Implementation
}
}
Shows API-specific formatting and dependency injection patterns.
Different file types need different rules:
# C# files
[*.cs]
indent_size = 4
# Project files
[*.{csproj,vbproj}]
indent_size = 2
.editorconfig is essential for maintaining code quality and consistency. The real-world examples demonstrate its effectiveness across different project types and team sizes. Start with these templates, customize to your needs, and let the tools work for you!
This comprehensive guide, backed by production code examples and practical workflows, provides everything needed to implement effective code styling in your .NET projects.
WRITTEN BY
Thuso Kharibe