Compile Time Style Enforcement
Enforce code style rules at compile time so the project fails to build if violations exist.
Supported Rules
Section titled “Supported Rules”| Rule | Description |
|---|---|
No var | Use explicit types instead of var |
| No unused usings | Remove unnecessary using directives |
| Collection expressions | Use simplified collection syntax like List<string> names = [] |
| CancellationToken forwarding | Forward CancellationToken to methods that accept it |
Configuration
Section titled “Configuration”EditorConfig
Section titled “EditorConfig”Create .editorconfig in the solution or project root:
root = true
[*.cs]# Use explicit types instead of var (IDE0008)csharp_style_var_for_built_in_types = false:errorcsharp_style_var_when_type_is_apparent = false:errorcsharp_style_var_elsewhere = false:errordotnet_diagnostic.IDE0008.severity = error
# Remove unnecessary usings (IDE0005)dotnet_diagnostic.IDE0005.severity = error
# Use collection expressions (IDE0028, IDE0300, IDE0305)dotnet_style_collection_initializer = true:errordotnet_style_prefer_collection_expression = true:errordotnet_diagnostic.IDE0028.severity = errordotnet_diagnostic.IDE0300.severity = errordotnet_diagnostic.IDE0305.severity = error
# Forward CancellationToken to methods (CA2016)dotnet_diagnostic.CA2016.severity = errorProject File
Section titled “Project File”Add these properties to your .csproj:
<PropertyGroup> <TreatWarningsAsErrors>true</TreatWarningsAsErrors> <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> <GenerateDocumentationFile>true</GenerateDocumentationFile> <NoWarn>$(NoWarn);1591</NoWarn></PropertyGroup>Property Reference
Section titled “Property Reference”| Property | Purpose |
|---|---|
TreatWarningsAsErrors | Fails build on any warning |
EnforceCodeStyleInBuild | Enables IDE rules (IDE0005, IDE0008, etc.) during build |
GenerateDocumentationFile | Required for IDE0005 (unused usings) to work |
NoWarn=1591 | Suppresses “missing XML comment” warnings from doc generation |
Auto-fix Violations
Section titled “Auto-fix Violations”Use dotnet format to automatically fix violations:
# Fix all style issuesdotnet format
# Fix a specific ruledotnet format --diagnostics IDE0008 --severity errorRule Details
Section titled “Rule Details”IDE0008 - Use explicit type
Section titled “IDE0008 - Use explicit type”// Badvar name = "Michael";var count = 42;
// Goodstring name = "Michael";int count = 42;IDE0005 - Remove unnecessary usings
Section titled “IDE0005 - Remove unnecessary usings”Removes using directives that are not referenced in the file.
IDE0028/IDE0300/IDE0305 - Collection expressions
Section titled “IDE0028/IDE0300/IDE0305 - Collection expressions”// BadList<string> names = new List<string>();List<int> numbers = new List<int> { 1, 2, 3 };
// GoodList<string> names = [];List<int> numbers = [1, 2, 3];CA2016 - Forward CancellationToken
Section titled “CA2016 - Forward CancellationToken”// Bad - cancellationToken exists but not forwardedasync Task DoWorkAsync(CancellationToken cancellationToken){ await client.SendAsync(request);}
// Goodasync Task DoWorkAsync(CancellationToken cancellationToken){ await client.SendAsync(request, cancellationToken);}