I'm getting some unexpected results when using the nullable reference types feature with generics.
For example, a generic class which has a nullable member
public class Entry<T> where T : notnull
{
public T? Member { get; set; }
}
If I try to assign a nullable int to Member
Entry<int> integers = new();
int? nullNumber = null;
integers.Member = nullNumber;
I get the exception:
Cannot convert source type 'System.Nullable<int>' to target type 'int'
I expect the types of int?
of the nullNumber
and int? Member
of the Entry
to be the same.
Why does the compiler think they are different?