📅 December 6, 2021

.NET Analyzers

I have been experimenting with .NET analyzers recently. Roslyn analyzers can inspect your C# code and highlight quality or style issues that you define. I have used Roslynator with success in the past for this. Roslynator provides hundreds of analyzers, refactorings, and fixes via its Visual Studio extension.

Starting with .NET 5, there are built-in analyzers that you can enable. Violations can be configured to show as suggestion, warning, or errors and appear with the prefix "CA" or "IDE" to differentiate them from compiler errors. You can configure the violations via an .editorconfig file. For example, if you want to receive errors in your code where using directives are unnecessary, you would use the following statement:

dotnet_diagnostic.IDE0005.severity = error

You can set the rule severity of all analyzers by using the following statement:

dotnet_analyzer_diagnostic.severity = <severity>

Below are some rules that I found to be helpful. Most rules have documentation associated with it. I've linked to the Microsoft docs for the first one as an example.

# development | dotnet