Skip to content

Treat Warnings as Errors

Some developers would prefer to be forced to fix warnings, so .NET provides a project setting to do this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>

The exceptions are the gRPC projects. This is due to a combination of factors. In .NET 7 or later, the compiler will warn if you compile source files that contain only lowercase letters in the name of a type.

The Google tools for generating C# source files from .proto files generate aliases for class names that only contain lowercase letters, which forces such projects to fail compilation. In this case it is necessary to disable this setting.

If you find that you get too many errors after enabling this, you can disable specific warnings by using the element with a comma-separated list of warning codes, as shown in the following markup:

<WarningsNotAsErrors>0219,CS8981</WarningsNotAsErrors>