0

Environment: vs 2019 v16.3.1

in c# netstandard2.0 and net451 project, I use c# 8 nulable reference with attributes.

Enable c# 8 in csproj:

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

In myclass.cs, I use AllowNullAttribute:

     class My class {
       [AllowNull]
     public T MyValue {get;set;}
     }

When building the project, I get a compilation error:

CS0246 The type or namespace name 'AllowNullAttribute' could not be found (are you missing a using directive or an assembly reference?) ConsoleApp1 (netstandard2.0)

The documentation Attributes extend type annotations include these types

What is wrong in this code?

M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • 2
    If I'm not mistaken. I don't think C# 8 is compatible with netstandard 2.0 and net451 – Kei Nov 15 '19 at 04:13
  • There is no reference in c# 8 docs to exclude these frameworks, see my [reference](https://learn.microsoft.com/en-us/dotnet/csharp/nullable-attributes#attributes-extend-type-annotations) in the question. – M.Hassan Nov 15 '19 at 04:17
  • @ Herohtar, thanks for link. It help to know the limitation for c#8 in net frameworks. – M.Hassan Nov 15 '19 at 05:15

1 Answers1

2

C# 8 (and therefore, by extension, AllowNull) does not seem to be fully supported in netstandard 2.0.

For example, if you go to the documentation for AllowNull and select .NET Standard 2.0 as the version, you will get the following message:

The requested page is not available for .NET Standard 2.0. You have been redirected to the newest product version this page is available for.

Also see this Github issue. Namely,

Officially, C# 8.0 is only supported on runtimes that adhere to the .NET Standard 2.1. That does not (and will not) include .NET Framework 4.x.

Kei
  • 1,026
  • 8
  • 15