104

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,073
  • 146
  • 84
  • 117
Ogglas
  • 50,115
  • 30
  • 272
  • 333
  • 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

9 Answers9

120

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,122
  • 18
  • 16
James LoForti
  • 1,268
  • 1
  • 7
  • 8
  • 33
    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://docs.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
89

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
  • 1
  • 4
  • 26
  • 35
  • 1
    Great answer, thorough =) – Ted Jul 05 '20 at 20:55
  • "Here is part of the ... Microsoft Docs article" — It's not "part of" that document; this answer contains that document almost _in its entirety_. See [How to reference material written by others](https://stackoverflow.com/help/referencing) where you'll not only learn that, basically, the second two-thirds of this answer should be one big blockquote, but also the instruction "Do _not_ copy the complete text of sources". That you've been making edits apparently to keep this in sync with the source text should have been a clue of the perils of making wholesale copies of documentation like this. – Lance U. Matthews Apr 12 '22 at 01:13
  • It's not clear how this is an/the answer, anyways, when, as you acknowledge, you're just repeating the same `.csproj` configuration back to the asker. – Lance U. Matthews Apr 12 '22 at 01:14
  • @LanceU.Matthews: Thank you for your comments. Your are right, it is a bad practice to quote a large part of an external source. I shortened the quote and added the last date the article was updated when quoted. – Eliahu Aaron Apr 12 '22 at 09:43
  • @LanceU.Matthews: As of how this answers the question, I pointed out at the beginning of the answer that different target frameworks can have different default language versions, so this may explain the problem the asker encountered. I may be wrong about that assumption, I don't know if the asker was using different target frameworks, but as you can see, my answer was selected as the correct answer by the asker. Besides, after encountering myself a similar problem, I thought this answer would be helpful to many others who encounter this problem. – Eliahu Aaron Apr 12 '22 at 09:44
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
  • 345
  • 3
  • 7
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
  • 131
  • 1
  • 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
  • 4,858
  • 4
  • 46
  • 84
  • 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
1

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

0

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

Pang
  • 9,073
  • 146
  • 84
  • 117
Cătălin Rădoi
  • 1,643
  • 19
  • 38
0

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

Popin
  • 18
  • 3
-1

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

<LangVersion>8.0</LangVersion>
Pang
  • 9,073
  • 146
  • 84
  • 117
Abdelrahman ELGAMAL
  • 386
  • 2
  • 6
  • 15