How to enable the built-in .NET Analyzers

By FoxLearn 2/4/2025 9:11:08 AM   124
.NET Analyzers are an essential tool for improving code quality by detecting potential issues, enforcing coding standards, and guiding best practices.

In previous versions of .NET, enabling .NET Analyzers required manual installation and configuration. However, with recent updates, .NET Analyzers are now automatically installed and enabled by default when creating a new project.

There are two primary scenarios when dealing with .NET Analyzers:

.NET Analyzers Are Already Installed

When you create a new project targeting the latest version of .NET, .NET Analyzers are included by default. You can verify this by checking the Analyzers tab under Project > Dependencies in Visual Studio.

To adjust the analyzer settings, you can enable or disable .NET Analyzers using the EnableNETAnalyzers setting in the .csproj (or .props) file. Additionally, you can control the analysis mode and level, like so:

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>net6.0</TargetFramework>
  <EnableNETAnalyzers>true</EnableNETAnalyzers>
  <AnalysisMode>AllEnabledByDefault</AnalysisMode>
  <AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>
  • EnableNETAnalyzers: Set to true to enable the analyzers.
  • AnalysisMode: Use AllEnabledByDefault to activate all built-in analyzers.
  • AnalysisLevel: You can set this to latest to use the most up-to-date analysis level.

Note: You might be able to configure these settings through the Code Analysis tab in Visual Studio. However, the .csproj approach is more reliable, and I recommend using it if you encounter any issues.

It's best to start with AnalysisMode=AllEnabledByDefault. As you encounter analysis warnings that aren't relevant to your project, you can disable them individually.

Note: Even if your project isn't targeting the latest version of .NET, you can still use the analyzers as long as you include the necessary package.

 

Installing .NET Analyzers in an Existing Project

If your project doesn't already have .NET Analyzers installed, you can add them manually by running the following command:

Install-Package Microsoft.CodeAnalysis.NetAnalyzers

Once installed, enable and configure .NET Analyzers by adding the following code to your .csproj file:

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>netcoreapp3.1</TargetFramework>
  <EnableNETAnalyzers>true</EnableNETAnalyzers>
  <AnalysisMode>AllEnabledByDefault</AnalysisMode>
  <AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>

This ensures that the analyzers are active and properly configured.

Checking If Code Analysis Works

Once you have enabled .NET Analyzers, you can verify that they are working by intentionally introducing a piece of code that will trigger a warning.

For example, try adding the following enum:

public enum Animals
{
  Dog = 1,
  Cat = 2
}

If everything is set up correctly, you should see a code analysis warning, such as:

CA1008: Add a member to Animals that has a value of zero with a suggested name of 'None'.

If you don't see any warnings, try rebuilding the project to ensure that the analyzers are functioning as expected.

By following these steps, you can take full advantage of .NET Analyzers, whether you're configuring them in a newly created project or adding them to an existing one. With this tool enabled, you can catch potential issues early and ensure a higher quality of code throughout your development process.