This can be because the compiler uses by default different C# language versions for different Target Frameworks.
To override the default C# language, add to project file (as suggested in question):
<PropertyGroup>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
or:
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Note: It is not recommended to use a language version newer than the default.
From C# language versioning - Microsoft Docs (as of 03/11/2022):
This default choice also ensures you don't use a language that requires types or runtime behavior not available in your target framework. Choosing a language version newer than the default can cause hard to diagnose compile-time and runtime errors.
See C# language versioning - Microsoft Docs for the default C# language versions for the different target frameworks and how to manually select the C# language version.
See also the stack overflow answer Does C# 8 support the .NET Framework? for more information on this topic.
Here is part of the C# language versioning - Microsoft Docs article (as of 03/11/2022) which explains about the default language versions for different target frameworks:
C# language versioning
The latest C# compiler determines a default language version based on your project's target framework or frameworks. Visual Studio doesn't provide a UI to change the value, but you can change it by editing the csproj file. The choice of default ensures that you use the latest language version compatible with your target framework. You benefit from access to the latest language features compatible with your project's target. This default choice also ensures you don't use a language that requires types or runtime behavior not available in your target framework. Choosing a language version newer than the default can cause hard to diagnose compile-time and runtime errors.
C# 10 is supported only on .NET 6 and newer versions. C# 9 is supported only on .NET 5 and newer versions. C# 8.0 is supported only on .NET Core 3.x and newer versions.
...
Defaults
The compiler determines a default based on these rules:
╔══════════════════╦═════════╦═════════════════════════════╗
║ Target framework ║ version ║ C# language version default ║
╠══════════════════╬═════════╬═════════════════════════════╣
║ .NET ║ 6.x ║ C# 10 ║
║ .NET ║ 5.x ║ C# 9.0 ║
║ .NET Core ║ 3.x ║ C# 8.0 ║
║ .NET Core ║ 2.x ║ C# 7.3 ║
║ .NET Standard ║ 2.1 ║ C# 8.0 ║
║ .NET Standard ║ 2.0 ║ C# 7.3 ║
║ .NET Standard ║ 1.x ║ C# 7.3 ║
║ .NET Framework ║ all ║ C# 7.3 ║
╚══════════════════╩═════════╩═════════════════════════════╝