9
votes

When I create a new Console App (.NET Framework 4.8), and try to use C# 8's nullable reference types, I see the following:

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context

And, I get this warning in my build output:

warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Understandable, I haven't enabled nullable reference types yet. I add the following to my .csproj:

<Nullable>enable</Nullable>

This makes the build warning disappear, but Visual Studio's IntelliSense keeps complaining.

How can I configure Visual Studio so it understands I'm using nullable reference types in my .NET Framework project, without using #nullable everywhere?


In contrast, if I create a new .NET Core 3.0 Console App, everything works as expected.

2
Sadly, C#8 features are avaiable for Net Core 3 only.Karel Kral
This is supposed to work: one, two. The possibly non-obvious step is that you have to close and re-open the solution or restart VS after you edited the project file so the IS parser gets a fresh look at it.Hans Passant
@Rudey Sorry, it looks I have mistaken. I tried to C# 8 and it didn't work for me until Core 3.Karel Kral
@Rudey this is a Visual Studio issue - Intellisense is a VS feature. Which VS version are you using?Panagiotis Kanavos
Which version of Visual Studio is this? And has it been updated?Lasse V. Karlsen

2 Answers

4
votes

How can I configure Visual Studio so it understands I'm using nullable reference types in my .NET Framework project, without using #nullable everywhere?

I'm afraid the answer is negative since this could be one issue about VS Intellisense.

I found msbuild(VS build system) can recognize the C#8.0 in .net framework4.8 projects well during build process. But Intellisense(one VS feature) can't, so I've reported it. If anyone is interested in this issue, more details about the issue see Intellisense can't recognize C#8.0 in project that targets .net framework 4.8.

The behavior is that Intellisense doesn't display the correct message for us in .net framework 4.8 project with C#8.0. And this is one issue which can only be handled by the fix. So there's no valid way to resolve this issue of Intellisense(a VS feature) unless the fix comes after we report this issue. We may Suppress or Configure CS8632's severity, but it's quite a bad idea..

0
votes

Try update vs2019 to 16.3.2, works for me