36

I have solution with couple .NET Standard projects in all I wanted to enable c# 8 and nullable like below:

<PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
  </PropertyGroup>

Note: These settings are found in your .csproj file.

The problem is that some projects are compiling fine and some have error:

Invalid 'nullable' value: 'Enable' for C# 7.3. Please use language version 'preview' or greater

I have Visual Studio 16.2 Preview 2 and .NET Core 3 Preview 6. Is this a bug in preview or I'm doing something wrong?

Bandito
  • 330
  • 4
  • 15
dnf
  • 1,659
  • 2
  • 16
  • 29
  • You have `8.0`, not `preview`. See [this page](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version) for how to specify `preview`, `latest`, etc... – Rufus L Jun 17 '19 at 18:45
  • With preview error message is the same – dnf Jun 17 '19 at 19:00
  • 1
    No repro. I use `8.0` too. Do you have a `global.json` in those failing projects that points to an earlier SDK? – Panagiotis Kanavos Jun 19 '19 at 10:09
  • For repro I have my code github - for examle this project is failing - https://github.com/bigdnf/HomeCenter/blob/Core3/Core/HomeCenter.Model/HomeCenter.Model.csproj. I don't recall any globals. – dnf Jun 19 '19 at 10:15

6 Answers6

33

In my case, I ran into this problem with Visual Studio 2022 when I changed the target framework from .NET Standard 2.1 to .NET Standard 2.0. I solved my problem by removing <Nullable>enable</Nullable> in the .csproj file and restarting Visual Studio.

Original .csproj file:

<PropertyGroup>
  <TargetFramework>netstandard2.1</TargetFramework>
  <Nullable>enable</Nullable>
</PropertyGroup>

New .csproj file:

<PropertyGroup>
  <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
datchung
  • 3,778
  • 1
  • 28
  • 29
  • Didn't need to restart Visual Studio 17.5.3. – Tom Andraszek Apr 21 '23 at 05:55
  • Wow. Most confusing, yet accurate error message I've ever seen! – Simon_Weaver Aug 17 '23 at 01:43
  • BTW if this is a new project you've just created which you'd already referenced from another project BEFORE you made this fix you may need to remove the reference and re-add it again. I had to do this to bypass some extremely annoying error messages about namespaces not being found. – Simon_Weaver Aug 17 '23 at 01:47
18

You should try <LangVersion>preview</LangVersion> as the error message suggests.

Avin Kavish
  • 8,317
  • 1
  • 21
  • 36
  • With preview error message is the same – dnf Jun 17 '19 at 19:00
  • 1
    `preview` *is* `8.0` at this point in time. – Panagiotis Kanavos Jun 19 '19 at 10:09
  • It is not recognized as 8.0 till after release. – Avin Kavish Jun 19 '19 at 10:13
  • If you right click on your project, navigate to "Properties" >> "Build" >> "Advanced" >> "Language Version" you will notice that visual studio does not provide UI to change the value, You may need to Navigate to the csproj file (by right click on each of your project in visual studio and select "Unload Project", to open up the csproj file) of all your projects under your solution and add preview. This settings will help your latest C# compiler determine the default language version based on your projects targets framework. – crakama Oct 21 '21 at 08:48
3

I had error like this "Invalid 'nullable' value: 'Enable' for C# 7.3. Please use language version '8.0' or greater" and I was able to solve it by changing order of specified targeted frameworks.

<TargetFrameworks>net6.0;net48</TargetFrameworks>

To

<TargetFrameworks>net48;net6.0</TargetFrameworks>

3

I had had the same issue when I have to downgrade compile-time supported version of C# language from 11.0 to 7.0. I resolved my issue for .net 7.0 project and was able to compile it within VS2022 by simple replacement from 'enable' to 'disable'.

See the final project configuration:

<PropertyGroup>
   <TargetFramework>net7.0</TargetFramework>
   <ImplicitUsings>disable</ImplicitUsings>
   <Nullable>disable</Nullable>
   <LangVersion>7.0</LangVersion>
</PropertyGroup>
saqw3r
  • 73
  • 6
2

There is another solution... add some conditions to your project file to only use the new nullable feature for the target frameworks that support it.

There is an excellent article here... and all credit to the author for the import code snippets below:

<!-- Set the LangVersion = 8 -->
<PropertyGroup>
    <TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
    <LangVersion>8.0</LangVersion>
</PropertyGroup>

<!-- Only enable nullable feature for the supported frameworks -->
<PropertyGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' ">
    <Nullable>enable</Nullable>
</PropertyGroup>

And then to hide warning messages in the unsupported frameworks:

<PropertyGroup Condition=" $(Nullable) != 'enable' ">
  <NoWarn>$(NoWarn);CS8632</NoWarn>
</PropertyGroup>
J-Rome
  • 155
  • 1
  • 8
1

To solve this,

  1. In visual studio, right click on your project file, go to Properties
  2. Build --> Events --> Advanced --> uncheck the Deterministic here

Now build the project.

Siyon DP
  • 504
  • 6
  • 20
Cracker
  • 11
  • 1