DEV Community

loading...

Discussion on: Interview question: how do nullable value types work? (C#)

Collapse
slavius profile image
Slavius

Hi, great series.

Maybe you could add a small code example to the first paragraph, something like:

// equals to 0 by default
// semantically equal to int outsideTemperature = 0;
// or int outsideTemperature = default;
int outsideTemperature;

// call function to get outside temperature
outsideTemperature = ReadOutsideTemperature();

Console.WriteLine($"Outside temp (in °C): {outsideTemperature}");
// now the dilemma is that the output can be 0 
// in either case the function succeeds or fails 
// (for whatever reason)
// this can be fixed by declaring outsideTemperature as int? 
// and checking if outsideTempreature != null before 
// printing the value and treating it as a result from the function 
// while in fact it can be the default assigned by a compiler