My projects are using <Nullable>enable</Nullable> by default so every file needs to comply with this rule.
The generated files however don't comply as backing fields aren't initialized (which is ok) and provoke the error
Non-nullable field '_title' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
This is not only for the title but also for every other generated backing filed, think of usage of [Reactive].
The easy solution is to annotate all generated files with
// <auto-generated/>
...code...
as nullable rules (and all other codestyle rules in general) don't apply to these.
The global nullable context does not apply for generated code files. Under either strategy, the nullable context is disabled for any source file marked as generated. This means any APIs in generated files are not annotated. There are four ways a file is marked as generated:
In the .editorconfig, specify generated_code = true in a section that applies to that file.
Put <auto-generated> or <auto-generated/> in a comment at the top of the file. It can be on any line in that comment, but the comment block must be the first element in the file.
Start the file name with TemporaryGeneratedFile_
End the file name with .designer.cs, .generated.cs, .g.cs, or .g.i.cs.
Generators can opt-in using the #nullable preprocessor directive.
My projects are using
<Nullable>enable</Nullable>
by default so every file needs to comply with this rule. The generated files however don't comply as backing fields aren't initialized (which is ok) and provoke the errorNon-nullable field '_title' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
This is not only for the title but also for every other generated backing filed, think of usage of[Reactive]
. The easy solution is to annotate all generated files withas nullable rules (and all other codestyle rules in general) don't apply to these.
https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contexts --> Important note
Generators can opt-in using the #nullable preprocessor directive.