Feature 'using declarations' is not available in C# 7.3. Please use language version 8.0 or greater - Error on one machine but works on another
Asked Answered
G

10

145

When using Visual Studio Enterprise 16.3.7 on two separate machines, one builds fine and the other machine throws the error:

Feature 'using declarations' is not available in C# 7.3. Please use language version 8.0 or greater.

enter image description here

enter image description here

This can easily be solved on the non-working machine by setting LangVersion in .csproj as suggested here https://mcmap.net/q/50234/-c-7-1-can-39-t-be-published or let Visual Studio automatically fix it like the screenshot above.

<LangVersion>8.0</LangVersion>

What I can't understand is why one machine builds fine without this line in .csproj and the other machine needs it?

Genevieve answered 30/10, 2019 at 11:14 Comment(8)
If you get such an error, it means it's not a C# 8 project, or you use a Resharper version with an analysis bugHellenhellene
What is the TargetFramework and LangVersion in your csproj?Hellenhellene
@PanagiotisKanavos Yes but the project builds on one machine and not on the other - that is what I do not understand. ReSharper is not used.Genevieve
This could mean that the 3.0 SDK is missing on one machine. What does dotnet --list-sdks show?Hellenhellene
@PanagiotisKanavos Target framework .NET Framework 4.6.1 and LangVersion is not set.Genevieve
Then you can't use C# 8, not without setting LangVersion explicitly.Even then, some features won't work. C# 8 is supported on .NET Core 3 as some features require runtime support. One of the is default interface membersHellenhellene
@PanagiotisKanavos SDK 3.0.100 installed and if I set LangVersion it works on both machines. However one machine could build it without LangVersion set. I can not understand that.Genevieve
Check that the LangVersion is set on the Debug and on the Build configuration. The two machines may be set to build different build configurations?Herbst
D
140

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                      ║
╚══════════════════╩═════════╩═════════════════════════════╝
Disforest answered 24/2, 2020 at 14:35 Comment(3)
Great answer I would add that .Net Core 3 and at least VS 2019 need to be installedViolation
it's a dirty workaround, but not a solution. answer from Sudharshann do the trick.Carlson
Assume if I want my project to target multiple versions, then I have to use the oldest compatible language version? For example in order to target: net6.0;net7.0;net8.0;netstandard2.0 I have to use C# 7.3 or utilise conditional compilation?Mitzi
B
153

I received the same error, but I had simply forgotten to include the

<LangVersion>8.0</LangVersion>

attribute in ALL the .csproj files in the solution. The following is my current c# 8 setup:

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
    <NullableContextOptions>enable</NullableContextOptions>
  </PropertyGroup>

I found the following documents to be the most helpful when migrating from core 2.2 to 3.x:

MSDN 2.2 -> 3.0

MSDN 3.0 -> 3.1

Baxley answered 1/1, 2020 at 18:30 Comment(6)
You can use <LangVersion>latest</LangVersion> instead of <LangVersion>8.0</LangVersion>Disforest
I think the first link MSDN 2.2 -> 3.0 is meant to link to the following: learn.microsoft.com/en-us/aspnet/core/migration/22-to-30 I've tried editing the answer to amend it, but "suggested edit queue is full", so will post here instead.Piffle
This answer definitely answers the question directly but I feel like it misses the fact that something else is probably wrong. By default, like the other answer mentions, the value is "latestMajor". The answer isnt to blindly hard code a version (which your compiler should be using if it is available) but to figure out why your compiler is choosing the wrong version.Whoops
Also, make sure to put <LangVersion> inside <PropertyGroup>, not inside <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == '<some specific config>' ">Imidazole
@Artemious I had this exact problem. Thanks! I was like I can see it says the right version, but it won't compile.Goodness
I came across this problem because somehow my 'debug' project file was set to 'latest', but 'release' was not. So my code targeting 4.8 ran fine in 'debug' mode, but gave compiler errors in 'release'. Changing it to 'latest' fixed the errors, and the code runs fine?!? That means some of these errors aren't legit?Purposive
D
140

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                      ║
╚══════════════════╩═════════╩═════════════════════════════╝
Disforest answered 24/2, 2020 at 14:35 Comment(3)
Great answer I would add that .Net Core 3 and at least VS 2019 need to be installedViolation
it's a dirty workaround, but not a solution. answer from Sudharshann do the trick.Carlson
Assume if I want my project to target multiple versions, then I have to use the oldest compatible language version? For example in order to target: net6.0;net7.0;net8.0;netstandard2.0 I have to use C# 7.3 or utilise conditional compilation?Mitzi
A
24

I had to update Visual Studio to version from 16.3.X to 16.4.2. This resolved the problem and I didn't have to add any LangVersion.

Credits: https://github.com/aspnet/AspNetCore.Docs/issues/16047

Apices answered 8/1, 2020 at 8:1 Comment(1)
This doesn't seem to work for .NET Framework - I'm on 16.7.3Panier
G
10

I downloaded the latest version of .Net Core 3.0 and 3.1 and had the same issue. For me, the fix seemed to download the latest update for Visual Studio 2019 (to version 16.4.2).

This also restarted my computer and the error went away.

Guidepost answered 31/12, 2019 at 17:3 Comment(2)
That is not a VS2019 version number, they look like 16.x.y. Use Help > About.Hubbard
4.7.03056 is a .NET Framework version, last VS version currently is 16.4.4Automation
W
9

2021

Cause: You may be targeting .NET Standard 2.0, which uses C# 7.3.

Fix: Under the project's Properties, click on the Application panel and choose .NET Standard 2.1 as the Target framework.

After the above change, Visual Studio 2019 will fix the issue by itself, and no LangVersion setting will be necessary.

See: C# language versioning

enter image description here

Washer answered 22/1, 2021 at 16:34 Comment(5)
This showed up when trying to use copied code in a new Xamarin shell app that seems to use .NET Standard 2.0 in its template. Changing the Target Framework to .NET 2.1 in Properties was indeed the fix.Forbidden
@Forbidden Glad it was of help to you. Regards.Washer
Do you know why the IDEs support a higher C# language level? I have a project targeting 4.8. In rider.net I can have C# 10 supported syntax and it works, but when I build the same project from the command line using msbuild it fails. Even if I set the languageversion to latest, it still doesn't compile.Mucus
@costa I am thinking it is because it will somehow still build through a combination of flags, tricks, hacks?Washer
@Washer - I fixed it, the <LangVersion> was not added to the release settings which is what I target when I use msbuild.Mucus
L
4

I did the following and it solved my issue:

  1. create a file named "Directory.Build.props" in the solution directory and writing this code:

    <Project>
     <PropertyGroup>
      <LangVersion>latest</LangVersion>
     </PropertyGroup>
    </Project>
    
  2. Deleted the .vs folder (it is hidden in the solution directory)

  3. Restart the Visual Studio

Liverish answered 18/1, 2021 at 12:2 Comment(0)
A
4

In my case, I had to remove to remove the <ImplicitUsings>enable</ImplicitUsings> line from my .csproj file.

Alexandria answered 5/12, 2022 at 19:56 Comment(1)
I've started from scratch with dotnet new classlib and dotnetstandard2.0. but dotnet msbuild -restore ... failed with error CS8370: Feature 'global using directive' is not available in C# 7.3. you saved my day!Carlson
B
0

If you install ReSharper, it will automatically modify the csproj files for you ;)

Bathe answered 30/8, 2021 at 12:43 Comment(0)
C
0

Check that you have valid configuration on both machines (Debug/Release, x64/Any CPU). This could also result to this error.

Citation answered 27/10, 2021 at 5:47 Comment(0)
D
-2

You must forget this tag in one of your PropertyGroup 's in your *.csproj file.

<LangVersion>8.0</LangVersion>
Dummy answered 17/8, 2021 at 11:12 Comment(1)
This fix is stated in the question itself.Lunde

© 2022 - 2024 — McMap. All rights reserved.