public class ClassA<TUid>
{
public TUid? Uid { get; set; }
}
public class ClassB : ClassA<Guid>
{
public void Test()
{
ClassB dto = new ClassB();
dto.Uid = null;
}
}
C# compiler doesn’t accept this C# code at the line: dto.Uid = null
saying that
Cannot convert null to ‘Guid’ because it is a non-nullable value type
Isn’t generic type TUid
in ClassA
substituted with Guid
in ClassB
so it should result in Guid?
like this?
public Guid? Uid { get; set; }
If I declare ClassA
as non-generic
public class ClassA
{
public TUid? Uid { get; set; }
}
or pass generic parameter as Guid?
public class ClassB : ClassA<Guid?>
then dto.Uid = null
doesn’t cause any problem.
What is it about the parameter type inference rules that confuses the compiler in the first example?