C# compiler throws Language Version (LangVersion) reference error "Invalid 'nullable' value: 'Enable' for C# 7.3"
Asked Answered
E

7

44

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?

Epidemic answered 17/6, 2019 at 18:36 Comment(4)
You have <LangVersion>8.0</LangVersion>, not <LangVersion>preview</LangVersion>. See this page for how to specify preview, latest, etc...Davies
With preview error message is the sameEpidemic
No repro. I use 8.0 too. Do you have a global.json in those failing projects that points to an earlier SDK?Vial
For repro I have my code github - for examle this project is failing - github.com/bigdnf/HomeCenter/blob/Core3/Core/HomeCenter.Model/…. I don't recall any globals.Epidemic
E
54

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>
Enigmatic answered 1/11, 2022 at 1:14 Comment(3)
Didn't need to restart Visual Studio 17.5.3.Suppress
Wow. Most confusing, yet accurate error message I've ever seen!Hasdrubal
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.Hasdrubal
H
18

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

Hollis answered 17/6, 2019 at 18:41 Comment(4)
With preview error message is the sameEpidemic
preview is 8.0 at this point in time.Vial
It is not recognized as 8.0 till after release.Hollis
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 <LangVersion>preview</LangVersion>. This settings will help your latest C# compiler determine the default language version based on your projects targets framework.Avowal
C
4

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>

Clearwing answered 27/9, 2022 at 10:33 Comment(0)
S
3

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>
Spokesman answered 8/12, 2022 at 16:2 Comment(0)
P
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>
Plague answered 19/1, 2023 at 11:8 Comment(0)
H
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.

Hendeca answered 9/2, 2023 at 11:54 Comment(0)
W
1

I was testing AvaloniaUI With version Visual studio 2022 and it must have changed it. Inside file "Directory.Build.props" was the property nullable enable

Just deleted the file and it works!

Weakminded answered 25/3 at 15:22 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Analects

© 2022 - 2024 — McMap. All rights reserved.