Why always null annotation

 So I've used resharper to add the "ambientLightSensor" parameter to the "ConnectSensor" method. And it does it with a null annotaion:

private void TryConnectLastUsedSensor()
{
if (_settings.LastUsedSensor == null) return;

var sensors = _sensor.GetSensors();
var lastUsedSensor = sensors.FirstOrDefault(s => s.FriendlyName == _settings.LastUsedSensor.FriendlyName);
if (lastUsedSensor == null) return;

ConnectSensor(lastUsedSensor);
}

private void ConnectSensor(AmbientLightSensor? ambientLightSensor)
{
IsSensorConnected = _sensorService.ConnectToSensor(SelectedSensor.FriendlyName);
if (IsSensorConnected) _settings.LastUsedSensor = SelectedSensor;
}

I don't understand why though. For one I check the object in the calling method if it's null and on the other hand Visual Studio tells me that "The annotation for nullable reference types should only be used in code within a '#nullable' annotations context." Is there a setting to turn this off?

1 comment
Comment actions Permalink

Hi Marco Rebsamen,

There is no specific setting to disable automatic nullable annotation. It is set according to the project language version and nullable context. 

Try lowering the C# version in the project. 

0

Please sign in to leave a comment.