相关文章推荐
温柔的鸵鸟  ·  中国能源报-人民网·  4 月前    · 
犯傻的海龟  ·  如何用random ...·  1 年前    · 
从容的圣诞树  ·  MySQL-17-mysql alter ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

AllowNull Attribute in c#8 for netstandard2.0 and net451 raise compilation error [duplicate]

Ask Question

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?

There is no reference in c# 8 docs to exclude these frameworks, see my reference in the question. – M.Hassan Nov 15, 2019 at 4:17

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

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