2

In Visual Studio 2019 I can set the Nullable Option in the Build-Properties to Annotation. When I read in the docs for this settings:

Variables of a reference type, string for example, are non-nullable. All nullability warnings are disabled.

Now I wonder what the exact use of this setting is. The variables are non-nullable, but there are no warnings. The Non-Nullable-Feature of C# 8 only generates warnings. Why is there a setting which says the variables are non-nullable but there are no warnings?

BennoDual
  • 5,865
  • 15
  • 67
  • 153
  • What they mean is a number type like int? where the question mark means nulllable. The doc say you can't use the qustion mark. – jdweng May 09 '21 at 17:17
  • As a reference (which hopefully give more info): https://stackoverflow.com/questions/55492214/the-annotation-for-nullable-reference-types-should-only-be-used-in-code-within-a – Luuk May 09 '21 at 18:02
  • @jdweng But when I set the property to annotation, `int? i = null;` is still possible – BennoDual May 09 '21 at 18:18
  • I think it refers to CONTEXT which is what Entity base class uses to connect to a database. I think it is to handle case like DateTime which cannot be null in Net but many databases do allow a DateTime to be null. – jdweng May 10 '21 at 00:28
  • @jdweng `Variables of a reference type, string for example, are non-nullable. All nullability warnings are disabled.` `int?` is not a a reference type, so I am not sure how that comment can relate to that. – mjwills May 10 '21 at 05:15
  • 1
    `What does Nullable Annotation exactly mean?` It means "I want to be able to use string? etc without any warnings". https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contexts – mjwills May 10 '21 at 05:17

1 Answers1

2

The #nullable enable annotations directive and the corresponding project setting are useful to add annotations to a public API before being ready to deal with warnings in your code.

In such a nullable context, string means "not-nullable string", but no warnings are produced if you assigned null to a variable of that type or if you dereference a variable of type string?.

Julien Couvreur
  • 4,473
  • 1
  • 30
  • 40