StyleCop Rules — Code Style the Build Enforces
Three people work on ReadingRoom. One puts using lines inside the namespace, one outside; one documents every method, one never does. Code review turns into a style debate. StyleCop ends the debate by letting the build check style, the same way the compiler checks types.
The rule
Agree on style once, in config; let the build point out breaks.
StyleCop.Analyzers is a NuGet package of Roslyn analyzers — small checks that run inside the C# compiler. Each rule has an id starting with SA and reports a diagnostic while you type and when you build.
dotnet add package StyleCop.AnalyzersRules fall into families: spacing, readability, ordering (for example, where using directives go), naming, maintainability, layout (braces and blank lines) and documentation. A few examples of what they catch:
- a public member with no XML documentation comment (
SA1600) - a file without the agreed header comment (
SA1633) - a field name that breaks the naming convention
You tune rules in .editorconfig, which the whole team shares:
[*.cs]
dotnet_diagnostic.SA1600.severity = none # we do not document every member
dotnet_diagnostic.SA1633.severity = none # no file headersA stylecop.json file holds finer options, such as the company name for headers. Add <TreatWarningsAsErrors>true</TreatWarningsAsErrors> to the project if style breaks should fail the build outright. dotnet format can fix many issues automatically.