public class MyClass : ISelfReferenceable<Guid>
{
public Guid Id {get;set;}
public Guid? ParentId {get;set;}
}
public interface ISelfReferenceable<TId>
{
TId Id {get;set;}
TId? ParentId {get;set;}
}
Can anyone explain to me why the above does not work?
It shouts Compiler Error CS0738
'type name' does not implement interface member 'member name'. 'method name' cannot implement 'interface member' because it does not have the matching return type of ' type name'.
on MyClass.ParentId
, saying that it doesn't implement ISelfReferenceable.ParentId
, unless I change MyClass.ParentId
from Guid?
to Guid
.
Currently, I get around this by using two generic types, one for Id
and another for ParentId
, or simply make ParentId
Guid
instead of Guid?
.
Is this the right approach and is there a better way of doing this?
CodePudding user response:
The problem is that TId?
is interpreted as a nullable reference type (in fact this is more complicated and depends on the language version and whether you are in a nullable context. See also the explanation under default constraint). You can fix it by adding a where TId : struct
generic type constraint to the interface. But of course this limits the applicable types to value types. A string
Id would not be possible.
The error also goes away if you declare
public class MyClass : ISelfReferenceable<string>
{
public string Id { get; set; }
public string? ParentId { get; set; }
}
CodePudding user response:
I don't know if it suits your purpose, but it compiles with this:
public interface ISelfReferenceable<TId> where TId : struct
{
TId Id {get;set;}
TId? ParentId {get;set;}
}
Remember that Guid
is a value type.