132

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://stackoverflow.com/a/48085575/3850405 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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Ogglas
  • 62,132
  • 37
  • 328
  • 418
  • If you get such an error, it means it's *not* a C# 8 project, or you use a Resharper version with an analysis bug – Panagiotis Kanavos Oct 30 '19 at 11:16
  • What is the TargetFramework and LangVersion in your csproj? – Panagiotis Kanavos Oct 30 '19 at 11:18
  • @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. – Ogglas Oct 30 '19 at 11:18
  • This could mean that the 3.0 SDK is missing on one machine. What does `dotnet --list-sdks` show? – Panagiotis Kanavos Oct 30 '19 at 11:19
  • @PanagiotisKanavos Target framework `.NET Framework 4.6.1` and `LangVersion` is not set. – Ogglas Oct 30 '19 at 11:19
  • 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 members – Panagiotis Kanavos Oct 30 '19 at 11:20
  • @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. – Ogglas Oct 30 '19 at 11:21
  • 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? – LorneCash Jul 21 '21 at 19:20

10 Answers10

139

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

Fábio Duque Silva
  • 2,146
  • 1
  • 18
  • 16
James LoForti
  • 1,456
  • 1
  • 8
  • 8
  • 44
    You can use `latest` instead of `8.0` – Eliahu Aaron Feb 13 '20 at 10:20
  • 2
    I think the first link `MSDN 2.2 -> 3.0` is meant to link to the following: https://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. – Andrew Hillier Apr 21 '20 at 07:26
  • 1
    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. – Marie Aug 06 '20 at 15:28
  • 7
    Also, make sure to put ` inside ``, not inside `` – Artemious Sep 23 '20 at 08:20
  • 1
    @Artemious I had this exact problem. Thanks! I was like I can see it says the right version, but it won't compile. – B.O.B. Aug 05 '21 at 15:04
  • 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? – Mmm Sep 27 '22 at 20:34
126

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                      ║
╚══════════════════╩═════════╩═════════════════════════════╝
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
23

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

tamsiv
  • 344
  • 4
  • 8
9

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.

Community
  • 1
  • 1
Curt
  • 161
  • 2
  • 4
7

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

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • 1
    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. – TLP Mar 04 '21 at 12:00
  • @TLP Glad it was of help to you. Regards. – Sabuncu Mar 04 '21 at 16:16
  • 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. – boggy Sep 13 '22 at 20:51
  • @costa I am thinking it is because it will somehow still build through a combination of flags, tricks, hacks? – Sabuncu Sep 14 '22 at 07:27
  • @Sabuncu - I fixed it, the was not added to the release settings which is what I target when I use msbuild. – boggy Sep 14 '22 at 21:15
3

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

1

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

Sudharshann D
  • 935
  • 9
  • 9
  • 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! – ursa Jul 04 '23 at 15:15
0

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Cătălin Rădoi
  • 1,804
  • 23
  • 43
0

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

Popin
  • 38
  • 4
-2

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

<LangVersion>8.0</LangVersion>
Pang
  • 9,564
  • 146
  • 81
  • 122
Abdelrahman ELGAMAL
  • 414
  • 2
  • 7
  • 15