Home > OS >  .Net 6: too many warnings for variables could be null
.Net 6: too many warnings for variables could be null

Time:08-11

I just upgraded a solution from .NET framework 4.8 to .NET 6. There are too many warnings, even after I enable nullable in project file

\<Nullable>enable\</Nullable>

Is it safe to suppress all of them?

Warning CS8600 Converting null literal or possible null value to non-nullable type.

Warning CS8601 Possible null reference assignment.

Warning CS8602 Dereference of a possibly null reference.

Warning CS8603 Possible null reference return.

Warning CS8604 Possible null reference argument for parameter

Warning CS8618 Non-nullable field '' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Warning CS8622 Nullability of reference types in type of parameter 'state' of '' doesn't match the target delegate 'WaitCallback' (possibly because of nullability attributes).

Warning CS8625 Cannot convert null literal to non-nullable reference type.

CodePudding user response:

is it safe to suppress all of them?

Safe is subjective. It will work like it always did; the same null reference bugs will exist with or without the warnings (and the number of those could be zero, if you're always perfect), so if you were happy without the warnings on .NET Framework: you are allowed to disable it.

The warnings are trying to help you, but I do accept that it can be overwhelming when enabling it on an existing large codebase. The option can also be controlled on a file-by-file basis via #nullable enable / #nullable disable if that helps. Personally, I've usually found it is worthwhile taking the time to process the warnings and fix any issues (which often just means marking some fields/parameters/locals as nullable, by adding a ?) - but: it is up to you whether you decide not to do that - whether than means doing it some time in the future, or never doing it.

  • Related