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
?
Generators can opt-in using the #nullable preprocessor directive. By default, nullable annotation and warning contexts are disabled. That means that your existing code compiles without changes and without generating any new warnings.
Nullable reference types aren't new class types, but rather annotations on existing reference types. The compiler uses those annotations to help you find potential null reference errors in your code. There's no runtime difference between a non-nullable reference type and a nullable reference type.
Although using nullable reference types can introduce its own set of problems, I still think it's beneficial because it helps you find potential bugs and allows you to better express your intent in the code. For new projects, I would recommend you enable the feature and do your best to write code without warnings.
Nullable type in C# is used to assign null values to value type variables like to the variables of type int, float, bool, etc., because they cannot store null values. On the other hand, we cannot use nullable with string or any other reference type variable because it can directly store null value.
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://docs.microsoft.com/en-us/dotnet/csharp/nullable-references
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With