2

I have a C# project which specifies the target framework but not the language version. Is there any documentation which specifies what language version will be chosen for any given target framework version? Also does the version of the SDK in use matter if the targeted framework is lower -- e.g. in my case, I am using .NET Core SDK 2.2 but the project targets netcoreapp2.1, would I get the same language version as if I was using SDK 2.1 targeting netcoreapp2.1? I'm currently getting a compilation error which states "Please use language version 7.2 or greater to allow ..." so I conclude 1) the default version is less than 7.2, and 2) the tooling supports or at least is aware of 7.2.

I found https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version states:

The C# compiler determines a default language version based on your project's target framework or frameworks. When your project targets a preview framework that has a corresponding preview language version, the language version used is the preview language version. When your project doesn't target a preview framework, the language version used is the latest minor version.

For example, during the preview period for .NET Core 3.0, any project that targets netcoreapp3.0 or netstandard2.1 (both in preview) will use the C# 8.0 language (also in preview). Projects targeting any released version will use C# 7.3 (the latest released version). This behavior means that any project targeting .NET Framework will use latest (C# 7.3).

There must be more to it than that, since the sentence " When your project doesn't target a preview framework, the language version used is the latest minor version." would mean to me that it should be using at least 7.2. Unless "latest minor version" really means latest minor version for the given targeted framework in which case I'd like to see a table somewhere that lists those (something like 2.1 -> 7.1, 2.2 -> 7.3, etc.).

After writing all of the above, I added <LangVersion>latest</LangVersion> to my csproj and it now compiles. I had been under the impression that the default for language version was "latest", but obviously that is not the case.

Community
  • 1
  • 1
user221592
  • 361
  • 1
  • 10
  • 1
    [Good explanation](https://stackoverflow.com/a/47595249/1797425) about it here. – Trevor Apr 24 '19 at 13:55
  • I don't believe that is was OP is asking. It looks like OP is asking about language version versus framework version. – Matthew Whited Apr 24 '19 at 14:33
  • I learned from the Q/A Codexer linked to that the default language version if not specified is the latest _major_ version (e.g. 7.0) not the latest minor version. That reflects the behavior I saw. So that was helpful. And unless I'm really reading it wrong, the Microsoft page I quoted above is incorrect, since it says it uses the latest minor version. – user221592 Apr 24 '19 at 16:09
  • 2
    But what I'm looking for is some documentation that tells for each combination of SDK version X and targeted framework Y, what language version do I get if: A) I don't specify LangVersion at all, B) I specify LangVersion "default", C) I specify LangVersion "latest". – user221592 Apr 24 '19 at 16:17
  • https://github.com/dotnet/core/blob/master/release-notes/releases.json describes the language version in each release, though it doesn't say if that's the "default" or "latest". – omajid Apr 24 '19 at 22:33

0 Answers0