Nullable reference types in .NET Framework projects not working with IntelliSense
Asked Answered
D

3

13

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.

Druggist answered 29/9, 2019 at 11:25 Comment(10)
Sadly, C#8 features are avaiable for Net Core 3 only.Moncear
@KarelKral do you have a source for that? I don't think that's true, .NET Framework projects are building fine when using nullable reference types. My only issue is that IntelliSense does not understand them.Druggist
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.Amoral
@Druggist Sorry, it looks I have mistaken. I tried to C# 8 and it didn't work for me until Core 3.Moncear
@Druggist this is a Visual Studio issue - Intellisense is a VS feature. Which VS version are you using?Heatherheatherly
Which version of Visual Studio is this? And has it been updated?Victim
I'm using Visual Studio Enterprise 2019, version 16.3.1.Druggist
@HansPassant The .NET teams have decided that this is not supposed to work: github.com/dotnet/project-system/issues/…Counteract
Hmya, "not supported" to Microsoft programmers doesn't mean "it doesn't work", it means that they can quickly close bug reports. So I guess we can do that as well, release 16.3.0 broke a lot of code.Amoral
@KarelKral See here for how it's possible to get it to work.Jolie
M
5

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..

Misquote answered 1/10, 2019 at 8:6 Comment(3)
It's a shame Microsoft appears to be doing nothing about this.Druggist
Just saw the update in DC, sorry for that inconvenience the C#8.0 is not fully supported in .net framework. You can get more details from this link :)Misquote
@Rudey, seems like it's because of relation between versions of .NET and C#. You're trying to use new feature with old .NET version that doesn't support that yet. If they did, then they would need to implement all the new features in all the old versions. Then versioning would loose any sence and competibility is questioniable for old projects.Sinegold
M
0

Try update vs2019 to 16.3.2, works for me

Ma answered 4/10, 2019 at 15:15 Comment(3)
Doesn't work for .NET Framework projects, which is what this question is about. The release notes for 16.3.2 also don't say anything about nullable reference types at all.Druggist
@Druggist I'm running VS Community 16.3.9 with a .NET Framework 4.7.2 project. It recognizes nullable annotations just fine.Jolie
Are you using old-style .csproj or new (SDK) .csproj format?Druggist
A
0

I would recommand to upgrade your project file (.csproj) to .netstandard2.0

You will have to target multiple version of the framework by modifing the .csproj.

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

You may also need to reference the package Microsoft.NETCore.Portable.Compatibility

By adding to your package references :

<PackageReference Include="Microsoft.NETCore.Portable.Compatibility" Version="1.0.1" />

I would recommand to read the following answer to understand if you really need it. When is it necessary to add the nuget package Microsoft.NETCore.Portable.Compatibility

Almshouse answered 21/2, 2023 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.