Sdílet prostřednictvím


Enable or install first-party .NET analyzers

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Overview

.NET compiler platform (Roslyn) analyzers inspect your C# or Visual Basic code for code quality and code style issues. The first-party .NET analyzers are target-platform agnostic. That is, your project doesn't need to target a specific .NET platform. The analyzers work for projects that target net5.0 and earlier .NET versions, such as netcoreapp, netstandard, and net472.

You can enable or install the first-party .NET analyzers in one of the following ways:

  • Enable from the .NET SDK: Starting in Visual Studio 2019 16.8 and .NET 5.0, these analyzers are included with the .NET SDK. Analysis is enabled, by default, for projects that target .NET 5.0 or later. You can enable code analysis on projects that target earlier .NET versions by setting the MSBUILD EnableNETAnalyzers property to true. You can also disable code analysis for your project by setting EnableNETAnalyzers to false.

  • Install as a NuGet package: If you don't want to move to the .NET 5+ SDK or if you prefer a NuGet package-based model, the analyzers are also available in the Microsoft.CodeAnalysis.NetAnalyzers NuGet package on Visual Studio 2019. You might prefer a package-based model for on-demand version updates. If you're on Visual Studio 2017, install the latest 2.9.x version of the Microsoft.CodeAnalysis.FxCopAnalyzers NuGet package instead.

Note

It is recommended that you enable the analyzers from the .NET SDK instead of installing the Microsoft.CodeAnalysis.NetAnalyzers NuGet package, when possible. Enabling the analyzers from the .NET SDK ensures that you automatically get the analyzer bug fixes and new analyzers as soon as you update the SDK. In the NuGet model, you need to update the NuGet package each time you want the latest bug fixes. The NuGet package is updated more frequently.

See also