Enforcing Consistent C# Code Style in Visual Studio with .editorconfig
Learn how to use .editorconfig in Visual Studio to enforce C# code style, trigger real‑time diagnostics, run Code Cleanup, and integrate style checks into CI pipelines.
23 Jul 2026, 19:19 UTC

The Problem: Inconsistent C# Style Across a Team
When multiple developers work on the same C# codebase, small formatting differences—like whether fields start with an underscore or how braces are placed—can clutter pull requests and make code reviews harder. Visual Studio can help enforce a shared style, but only if the team agrees on where the rules live and how they are applied.
How .editorconfig Works in Visual Studio
Visual Studio reads .editorconfig files from the repository root down to the folder containing the current file. Settings in a file closer to the source override those higher up, allowing a project‑wide baseline with occasional per‑folder exceptions. The IDE treats each rule as a diagnostic: violations appear as squiggles in the editor and entries in the Error List, with severity levels (suggestion, warning, error) defined directly in the config.
Only settings exposed by the C# language service or Roslyn analyzers are honored; unsupported keys are silently ignored. This means you can control naming conventions, brace layout, using‑directive sorting, and file headers, but not IDE themes, fonts, or keyboard shortcuts.
Example Configuration
# .editorconfig placed at the repository root
root = true
# Naming rule: private fields must start with '_'
dotnet_naming_rule.fields_should_begin_with_underscore.symbols = field
dotnet_naming_rule.fields_should_begin_with_underscore.style = field_style_underscore_preferred
dotnet_naming_rule.fields_should_begin_with_underscore.severity = error
dotnet_naming_style.field_style_underscore_preferred.capitalization = camel_case
dotnet_naming_style.field_style_underscore_preferred.required_prefix = _
# Brace style for methods
csharp_style_brace_disposal = true:warning
csharp_style_brace_method = true:warning
# File header (optional)
file_header_template = /*\n * Copyright (c) ${year} ${company}
*/
Save this file, then open any C# file in Visual Studio. If a private field lacks the leading underscore, you’ll see a red squiggle (because we set severity = error) and an entry in the Error List.
Using Code Cleanup to Apply Rules Automatically
Visual Studio’s Code Cleanup feature (shortcut Ctrl+K, Ctrl+E) can apply the formatting rules defined in .editorconfig without manual tweaking. It sorts using directives, adds file headers, and adjusts brace placement according to the config. You can invoke it on a single file, a project, or the whole solution.
Because Code Cleanup respects the severity levels, it will not change code that would violate an error‑level rule unless you explicitly allow it; instead, it flags the violation for you to fix.
CI Integration Trade‑offs
Local IDE enforcement is useful, but builds on a server should also fail when the style is broken. There are two common approaches:
- Enable Roslyn analyzers during the build – set
Severity = errorin.editorconfigand ensure the project SDK (Microsoft.NET.Sdk) is used. The compiler will treat the rule as a build error, causing the CI pipeline to stop. - Run a formatter verification step – add a script that executes
dotnet format --verify-no-changes. This command compares the working tree with the formatted output and exits with a non‑zero code if any differences exist.
Both approaches require the build agent to have the .NET SDK installed and appropriate permissions to read the repository. The analyzer method fails fast but only works for rules exposed by analyzers; the formatter method catches layout issues that analyzers might miss, but it adds an extra step to the pipeline.
Limitations and Practical Verification
Limitations:
- .editorconfig does not affect IDE preferences such as theme, font size, or key bindings.
- Some EditorConfig keys are ignored by the Visual Studio C# service (e.g., certain indentation values for specific languages).
- If you set a rule to
errorand existing code violates it, the build will break until the code is fixed.
How to check that the configuration works:
- Open a C# file that intentionally breaks a rule (e.g., a private field named
myField). Verify a red squiggle appears and the Error List shows the expected error. - Run
Ctrl+K,Ctrl+E(Code Cleanup) and confirm the field is renamed to_myFieldor that the IDE refuses to apply the change because of the error‑level setting. - In a terminal with repository read/write access, execute:
dotnet format --verify-no-changes
If the output reports no changes, the formatter agrees with your .editorconfig. If it lists files that would be changed, either update those files or adjust the config.
Actionable Closing
Start by adding a minimal .editorconfig at the root of your repository with a single naming rule set to error. Use the Error List to confirm that Visual Studio surfaces the violation locally, then decide whether to enforce the rule via analyzer failures in your CI or add a dotnet format verification step. This gives you immediate feedback in the editor and a reliable gate in your build pipeline, keeping the team’s C# style consistent without relying on manual code‑review comments.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.