Skip to the workspace
Pebellslearn by building
C# and .NET Basics  ›  Capstone Project

StyleCop Rules — Code Style the Build Enforces

StyleCop Rules 12 minStyleCop analyzers turn code-style conventions into build warnings, configured per project so every contributor writes C# the same way.

aspnet core / StyleCop Rules — Code Style the Build Enforces

Explore each scene at your own pace.

01 · The rule

The rule

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.Analyzers

Rules 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 headers

A 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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.