❔ How to make a generic class property nullable?
I'm trying to have a generic interface with a nullable property:
But this doesn't work, as when I add it to a class, it gives an error when I try to make the implementation nullable aswell:
I says that
Foo
doesn't implement CreatorId
, and it only works when I have it as Guid CreatorId
Any idea why this doesn't work? is it even possible to this is C#? 🤔5 Replies
Since
TKey
isn't constrained to anything, if you have IMayHaveCreator<Guid>
then the CreatorId
will just have the type Guid
, not a nullable one
If you were to do where TKey : struct
then you would be able to have Guid?
, but then you could not use IMayHaveCreator
with reference types.
This is just an inherent limitation of how nullables are implemented in C#, sadly.I see. Thank you.
Adding
where TKey: struct
works for me for now.
If the issue with reference types becomes and issue in the issue, I'll find another solution
@🌈 Thinker 🌈 Thank you again 😃np
where TKey : notnull
should also workWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.