Given the following code samples:
class Value { }
Value? value = null;
if (value is not null)
{
Value value2 = value;
}
int? value = null;
if (value is not null)
{
int value2 = value; // CS0266: Cannot implicitly convert type 'int?' to 'int'.
}
I understand the issue has to do with reference types vs value types.
However, in both cases I am checking whether value
is null, so why can the compiler not understand that value should never be null inside the if block in the second example?
I can remedy the issue by using value.Value
, but this seems counterintuitive.
Help would be appreciated.