Home

Awesome

CSharp Guidelines Analyzer

Build status codecov

This Visual Studio analyzer supports you in making your code comply with the C# coding guidelines at CSharpGuidelines.

Note that many guidelines are already covered by Resharper, for which a layer file is provided. See Overview for the list of supported rules.

Analyzer in action

Get started

The latest version requires Visual Studio 2017 with Update 5 or higher. To get instant feedback on all files in your solution, activate Full Solution Analysis.

Rule configuration

The behavior of a few rules can optionally be customized using a configuration file. See documentation for details.

Suppressing rules

Rule warnings can be suppressed at various scopes, ranging from per line to at the project or solution level.

#pragma warning disable AV1532 // Loop statement contains nested loop
    foreach (string item in itemArray)
#pragma warning restore AV1532 // Loop statement contains nested loop

On the location of a warning, press Ctrl+. or Alt+Enter and select Suppress, in Source.

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Maintainability", "AV1532:Loop statement contains nested loop", Justification = "<Pending>", Scope = "member", Target = "~M:CSharpGuidelinesDemo.Demo.RunDemo(System.String[][],System.Boolean,System.String)~System.Collections.Generic.List{System.String}")]

On the location of a warning, press Ctrl+. or Alt+Enter and select Suppress, in Suppression File. Note that you can broaden the suppression scope by removing the Target and/or Scope attributes:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Maintainability", "AV1532:Loop statement contains nested loop", Justification = "<Pending>")]
root = true

[*.cs]
dotnet_diagnostic.av1115.severity = error
dotnet_diagnostic.av1130.severity = suggestion

Right-click your project, select Properties, tab Code Analysis. Click Open, expand CSharpGuidelinesAnalyzers and uncheck the rules you want to disable. When you save changes, a .ruleset file is added to your project.

Alternatively, navigate to your project in Solution Explorer and expand References, Analyzers, CSharpGuidelinesAnalyzer. Then right-click on one of the rules and select Set Rule Set Severity. These changes are stored in a .ruleset file in your project.

To apply the custom ruleset to the entire solution, move the .ruleset file next to your .sln file and browse to it on the CodeAnalysis tab for each project.

Performance

If you run these analyzers on a large codebase and are concerned about performance, consider disabling AV1568 and AV1739. These two are by far the most resource-intensive.

Contribute!

The analyzers in this project benefit a lot from testing on various codebases. Some of the best ways to contribute are to try things out, file bugs, and join in design conversations.

Trying out the latest build

After each commit, a new prerelease NuGet package is automatically published to AppVeyor at https://ci.appveyor.com/project/bkoelman/csharpguidelinesanalyzer/branch/master/artifacts. To try it out, follow the next steps:

Building from source

Clone the repository and open CSharpGuidelinesAnalyzer.sln in Visual Studio. You can now build and run the tests.

To debug an analyzer, set a breakpoint and press F5. This launches a second (experimental) Visual Studio instance with the debugger attached. In the experimental instance, open a project and observe your breakpoint get hit.

Note: When using Visual Studio 2022 or higher, breakpoints don't get hit unless you change the following setting in the experimental instance: