CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Nullability - TryGet dereference of a possibly null reference

Open dotTrench opened this issue 3 years ago • 1 comments

Describe the bug When using the TryGetField methods with nullability turned on the out field is always considered nullable regardless if the method returns true or false.

To Reproduce

var csvReader = CreateCsvReader();
if (csvReader.TryGetField<string>("MyField", out var data))
{
    var mySubstring = data.Substring(0, 10); // [CS8602] Dereference of a possibly null reference.
}

Expected behavior if the return value from TryGetField returns true the out field shouldn't be considered nullable.

Additional context I assume this can be achieved by adding the NotNullWhenAttribute to the TryRead methods in IReaderRow and CsvReader methods for frameworks where nullability is enabled e.g.

bool TryGetField<T>(string column, [NotNullWhen(true)] out T? field);

dotTrench avatar Oct 19 '22 09:10 dotTrench

Oh interesting. I'm sure there are a lot of places I'll need to do this. I have nullable stuff partially in place. Takes a while to do retroactively though.

JoshClose avatar Oct 19 '22 17:10 JoshClose