Microsoft .NET 5 adds safe warnings for C#
Microsoft’s planned .NET 5 software development platform produces safe warnings for the C# compiler and automatically finds latent bugs.
With .NET 5, a new capability called AnalysisLevel introduces warnings in a way that alerts developers that something is wrong. The default AnalysisLevel for all projects targeting .NET 5 will be set to 5, which provides new compiler language analysis. This means that more warnings and suggestions to fix them will be introduced.
Microsoft has had many instances over the years where it wanted to provide warnings that something was wrong with a C# coding project, ranging from common coding mistakes to API misuse patterns. But the company has been reluctant to do so because of the breaking change issue. With AnalysisLevel, warnings are introduced for patterns in a safe way.
.NET 5 currently is in a preview stage, with the production version due in November. The company detailed its C# compiler warnings plan in a bulletin posted on August 27. The bulletin noted that all new .NET projects will be opted into AnalysisLevel 5. Here are some new level 5 warnings and suggestions for .NET 5:
- CA1416, an interoperability warning to warn when code does not work across all platforms.
- CA2247, a usage warning rethrow to preserve stack details.
- CSO185, a correctness error to not allow locks on non-reference types.
Previously, .NET required the installation of NuGet packages or other standalone tools to enable deeper code analysis. The .NET 5 SDK provides this automatically. Future plans call for a new AnalysisLevel for every release of .NET. This is intended to ensure that a given analysis level always represents the same set of analysis defaults — the rules and their severities. Turning on an existing rule by default would be done in an upcoming AnalysisLevel instead of changing the existing level. This ensures that a given project produces the same warning regardless of the SDK version, unless the project uses preview
or latest
.