Proj0048: Language version should be set to an explicit version number
When <LangVersion>
is not explicitly defined, the language version used by the compiler will depend on the language version associated with the target framework.
This can result in unintentional side effects when targeting multiple target frameworks or when changing the target framework version.
When <LangVersion>
is set to latest
, latestMajor
, default
or preview
, the actually used language version may be different depending on the .NET compiler version. This means that for different members in the same team, the used language version may be different depending on which .NET versions they have installed locally. This results in builds that are harder to reproduce.
To ensure the language version used by the compiler is not unintentionally changed, it is advised to explicitly define the <LangVersion>
node with an explicit version number.
For more information and a list of valid LangVersion
values, please consult this document.
Non-compliant
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
Or:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
</Project>
Compliant
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12.0</LangVersion>
</PropertyGroup>
</Project>