2

So I'm trying to utilize C# 8 in my .NET 4 web application by adding two entries in my .csproj file right inside the PropertyGroup node.

<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>

But for some reason when I try to do string? name = ""; I still get an error stating "Error CS8370 Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater."

I'm using Visual Studio 2022 17.0.4.

Why is it that I get this error?

Mark Denom
  • 987
  • 1
  • 8
  • 24
  • C# 8 is only available/supported (by default) when targetting .NET Standard 2.1 or .NET Core 3+ - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version – phuzi Mar 03 '22 at 13:00
  • There was [some possibility](https://stackoverflow.com/a/57020770/4137916) for the combination of .NET 4/C# 8 in Visual Studio versions of yore, albeit unsupported, but it may well be that VS 2022 does away with even this partial support (although it should arguably use a clearer error message then). Certainly NRTs are not fully supported on .NET 4 even where VS does allow the combo. – Jeroen Mostert Mar 03 '22 at 13:04
  • 1
    @phuzi That's not true - you can certainly use C# 8 with a .net 4.8 assembly. – Matthew Watson Mar 03 '22 at 13:13
  • @MatthewWatson I did say "by default" There is information on the linked page that describes how to override the default. – phuzi Mar 03 '22 at 13:20
  • @phuzi Yes, but look at the OP's project setting `8.0`. They are *already* enabling C# 8. – Matthew Watson Mar 03 '22 at 13:24
  • 1
    Can you post your project file? – Matthew Watson Mar 03 '22 at 15:22
  • @MatthewWatson did ask you to show us your complete project file, but you haven't yet. – phuzi Mar 07 '22 at 09:06
  • May I know whether your issue have been solved or not? if not, please share it in here, we can work together to figure it out. – Jingmiao Xu-MSFT Mar 21 '22 at 06:40

1 Answers1

2

If you want to use nullable reference types in .NET 4.0 web application, you can refer to the following steps: First, unload your project, right-click and choose Edit Project File to open .csproj file. enter image description here

Second, add <LangVersion>8.0</LangVersion> in the code: enter image description here

Finally, reload project and add #nullable enable at the top of the file. enter image description here

Jingmiao Xu-MSFT
  • 2,076
  • 1
  • 3
  • 10