What is .editorconfig?
.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.
Key Benefits
- Consistent code style across projects
- IDE-agnostic configuration
- Enforced standards through compiler warnings/errors
- Reduced code review friction
- Automated formatting on save
Core Concepts
File Structure
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
Basic Settings
Essential configurations include:
- indent_style (tabs vs spaces)
- indent_size
- charset
- end_of_line
- insert_final_newline
- trim_trailing_whitespace
Naming Conventions
.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
Real-World Examples from Production Code
MAUI Project Configuration
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
Enterprise .NET Backend
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
Blazor Frontend
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
Project-Specific Customization
Different project types benefit from tailored rules:
MAUI Specific
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.
API Controllers
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
public async Task<IActionResult> GetForecast()
{
// Implementation
}
}
Shows API-specific formatting and dependency injection patterns.
Advanced Features
Severity Levels
- error: Must be fixed to compile
- warning: Should be fixed
- suggestion: Consider fixing
- silent: Documentation only
File-Specific Rules
Different file types need different rules:
# C# files
[*.cs]
indent_size = 4
# Project files
[*.{csproj,vbproj}]
indent_size = 2
IDE Integration
Rider Code Cleanup Options
- Full Cleanup
- Complete code standardization
- Applies all formatting rules
- Perfect for new projects
- Reformat Code
- Quick visual formatting
- Ideal for quick fixes
- Safe for small changes
- Reformat & Apply Syntax Style
- Balanced approach
- Updates code patterns
- Good for regular maintenance
Best Practices
- Start with a comprehensive template
- Customize based on team preferences
- Document any deviations from standards
- Regular code cleanup passes
- Include .editorconfig in source control
Team Workflow Integration
- Configure IDE settings
- Run cleanup before commits
- Validate in CI/CD pipeline
- Review formatting in PRs
- Regular team standard reviews
Conclusion
.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.