8

I've been playing with new c# 8.0 NullableReferenceTypes feature for a while. And I'm little bit confused about turning this feature on via changing .csproj file.

I mean in order to enable this feature you have to put the following lines in your .csproj file:

<PropertyGroup>
  <LangVersion>8.0</LangVersion>
  <NullableContextOptions>enable</NullableContextOptions>
</PropertyGroup>

But also I've found another one way:

<PropertyGroup>
  <LangVersion>8.0</LangVersion>
  <NullableReferenceTypes>true</NullableReferenceTypes>
</PropertyGroup>

So it almost the same but anyway. What is the difference between NullableContextOptions and NullableReferenceTypes?

2
  • This may help you. Feb 24, 2019 at 18:36
  • Since VS 16.1 the property was renamed to Nullable. Neither NullableContextOptions nor NullableReferenceTypes are valid any more. The name is shorter and aligns with the command line argument. May 23, 2019 at 3:33

3 Answers 3

8

In earlier previews, the compiler only allowed two settings (true/false, meaning on/off).

In later previews (starting with preview 2), the compiler allowed more options, so the name of the setting and the possible options were changed.

Here's the documentation of the possible options and their meanings: https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references

1
  • 1
    Note that the property name is now simply <Nullable>. May 23, 2019 at 3:04
4

As of .NET Conf 2019 (C# 8.0 release), the correct way to achieve default non-nullable reference types is:

<PropertyGroup>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
</PropertyGroup>
0

you need to have Visual Studio 2019 preview 2, or .NET Core 3.0 preview 2 in order to use NullableReferenceTypes.

You also need to enable C# 8.0. Check the LangVersion element to make sure its set to 8.0.

NullableReferenceTypes is changed to <NullableContextOptions>enable</NullableContextOptions> in Visual Studio 2019 preview 2

PS: this feature does not yet work in .Net Framework

5
  • what do you mean this feature does not yet work in .net framework ? I can use the following line to enable this feature for my .net framework app: #nullable enable. And it actually works well. But I have no idea how to enable it via .csproj file.
    – isxaker
    Feb 24, 2019 at 19:08
  • there is an issue here Feb 24, 2019 at 20:53
  • basically I created it yesterday )
    – isxaker
    Feb 25, 2019 at 8:56
  • @DervişKayımbaşıoğlu What do you mean "this feature does not yet work in .Net Framework" ? It works fine for me.
    – Konrad
    Mar 15, 2019 at 9:30
  • 1
    @DervişKayımbaşıoğlu it works only with new csproj format, otherwise <NullableContextOptions> is ignored.
    – Konrad
    Mar 15, 2019 at 10:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.