C# 8.0 adds new syntax for tracking nullability of fields: by default, fields cannot be null, but if the type is suffixed with ?, then it can be nullable. (Note that this is identical to the syntax that produces a nullable value type.) This lets the compiler force us to not make various null pointer errors; in exchange, we have to do some mass refactoring.
At the start, we'll enable nullability enforcement project-wide, but then mark (almost?) every file as #pragma nullable disable, which is a TODO mark of a sort until we migrate those files. The goal is to get every file nullable-correct with no remaining #pragma nullable disable directives.
To convert from a nullable type to a non-nullable type in code - that is, we genuinely have a value that might be null, and we need to send it to somewhere we can't have a null - we should add the null-check we should have had in the first place (and nullability annotations just surfaced the necessity of). If we are very certain a field cannot be null at a certain point despite its nullable type and the compiler not identifying the context as guaranteed nonnull, there is a "nonnull asserting" syntax (it uses exclamation marks in weird spots), but I want at least a comment describing why we are confident in the nonnullability assertion everywhere we use one.
C# 8.0 adds new syntax for tracking nullability of fields: by default, fields cannot be null, but if the type is suffixed with
?
, then it can be nullable. (Note that this is identical to the syntax that produces a nullable value type.) This lets the compiler force us to not make various null pointer errors; in exchange, we have to do some mass refactoring.At the start, we'll enable nullability enforcement project-wide, but then mark (almost?) every file as
#pragma nullable disable
, which is a TODO mark of a sort until we migrate those files. The goal is to get every file nullable-correct with no remaining#pragma nullable disable
directives.To convert from a nullable type to a non-nullable type in code - that is, we genuinely have a value that might be null, and we need to send it to somewhere we can't have a null - we should add the null-check we should have had in the first place (and nullability annotations just surfaced the necessity of). If we are very certain a field cannot be null at a certain point despite its nullable type and the compiler not identifying the context as guaranteed nonnull, there is a "nonnull asserting" syntax (it uses exclamation marks in weird spots), but I want at least a comment describing why we are confident in the nonnullability assertion everywhere we use one.
https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references has more information.