Using EditorConfig in Visual Studio to Enforce C# Code Style
Move C# style rules into a version‑controlled .editorconfig file so Visual Studio enforces indentation, var preferences, expression‑bodied members, and naming conventions automatically—reducing pull‑request noise and keeping the team consistent.
06 Sept 2025, 18:46 UTC

The Problem: Formatting Noise in Pull Requests
Every C# team eventually encounters the same friction: one developer indents with tabs, another with spaces; some prefer var, others explicit types; using directives end up sorted differently in every file. These differences do not break builds, but they clutter pull requests with formatting noise and make code reviews harder to read. Visual Studio’s built-in EditorConfig support moves style rules into a version‑controlled file that the editor enforces automatically, letting the team focus on logic rather than formatting.
EditorConfig Basics and Visual Studio Support
EditorConfig is an open standard that defines coding styles in a plain‑text .editorconfig file. When Visual Studio detects this file in a solution or project folder, it applies the rules to the editor, formatting commands, and Code Cleanup profiles. Support has been built into Visual Studio since 2017; richer .NET‑specific keys were added in Visual Studio 2019 version 16.3 and later, and Visual Studio 2022 continues to extend the set. Behavior depends on the installed workload and the .NET SDK version—older project formats (e.g., packages.config, .NET Framework 4.7.2 without SDK‑style projects) may ignore some keys.
EditorConfig uses INI‑style sections to match file patterns. A top‑level section sets baseline whitespace rules for all files, while [*.cs] sections add C#‑specific conventions powered by Roslyn analyzers.
Worked Example: Team‑Standard .editorconfig
Place this file at the repository root (next to the .sln) so it applies to every project:
# Top‑level settings for all files
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
# C#‑specific rules
[*.cs]
# Indentation already covered, can override if needed
# indent_size = 4
# Code‑style preferences (severity suffix controls UI)
csharp_style_var_elsewhere = true:suggestion
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_expression_bodied_methods = true:when_on_single_line
csharp_style_expression_bodied_properties = true:when_on_single_line
csharp_style_expression_bodied_lambdas = true:when_on_single_line
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Naming conventions (public members PascalCase)
dotnet_naming_rule.public_members_must_be_pascal_case.severity = suggestion
dotnet_naming_rule.public_members_must_be_pascal_case.symbols = public_symbols
dotnet_naming_rule.public_members_must_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.public_symbols.applicable_kinds = property,method,field,event
dotnet_naming_symbols.public_symbols.applicable_accessibilities = public
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Using directives
dotnet_sort_system_directives_first = true
root = truestops EditorConfig from searching parent directories outside the repository.- Whitespace rules (
indent_style,end_of_line) apply to every file type. - C# rules use the
:severitysuffix:suggestionshows a gray ellipsis (Alt+Enter to fix),warningshows a green squiggle,errorshows red and can be enforced in CI viadotnet format --verify-no-changes. when_on_single_linefor expression‑bodied members means the rule only triggers when the member fits on one line.- The naming‑convention block demonstrates the verbose but flexible Roslyn naming API—public members must be PascalCase.
Applying the Rules: Format Document and Code Cleanup
With the file in place, two workflows keep code consistent:
- Format Document (Ctrl+K, Ctrl+D or Edit > Advanced > Format Document) applies whitespace rules (indentation, line endings, trailing spaces) to the current file instantly.
- Code Cleanup (Edit > Code Cleanup or Ctrl+K, Ctrl+E) runs a configurable profile that can also apply code‑style rules like sorting using directives, converting to expression‑bodied members, and enforcing
varpreferences. Create a custom profile under Tools > Options > Text Editor > Code Cleanup to include the rules you want.
Run Code Cleanup on a single file, a selection in Solution Explorer, or the entire solution before committing. The changes appear as normal edits—review them, then commit.
Trade‑offs and Limitations
- Not every rule is honored equally. Whitespace rules are enforced by the editor core and work everywhere. Code‑style rules (
var, expression‑bodied, naming) rely on Roslyn analyzers shipped with the .NET SDK. Older project formats may ignore certain keys. - Severity matters. Using
:suggestionkeeps the team unblocked—developers see hints but can ignore them. Switch to:warningor:erroronly after the team agrees and CI is configured to fail on violations. - Cross‑IDE consistency. Rider, VS Code (with the C# extension), and other editors also read
.editorconfig, but their support for .NET naming rules varies. Test the file in each IDE your team uses. - Generated code. Add a section like
[**/*.g.cs]or[**/obj/**]withindent_size = 4only, disabling style rules for generated files to avoid noisy diffs.
Verify It Works
- Add the
.editorconfigfile to the repo root and commit. - Open any
.csfile in Visual Studio. Type a method with tabs instead of spaces—the editor should insert spaces immediately. - Run Code Cleanup on a file that has unsorted using directives or explicit types where
varis preferred. Confirm the changes match the configured rules. - Check Tools > Options > Text Editor > EditorConfig to ensure "Enable EditorConfig support" is checked (it is by default).
Closing: Make It Part of the Workflow
EditorConfig moves style decisions out of conversations and into the repository. Start with whitespace rules only—they’re universal and non‑controversial. Add one code‑style rule per sprint, discuss the diffs in review, and promote severity when the team is comfortable. Pair the file with dotnet format --verify-no-changes in your CI pipeline to catch drift before it merges. The result: pull requests that show logic changes, not formatting arguments.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.