I’d like to define an generic interface that allows nullable value types (int?, double?, etc.) and class types (which can also be null). I do not want to allow a simple value type. Is there a way to do this?
I’d like to define an generic interface that allows nullable value types (int?, double?,
Share
EDIT: Given the question title, I assume you want to constrain the type parameter to not be a non-nullable value type. It would probably be a good idea to specify that in the question body too.
No – there’s no such constraint. In fact, both
classandstructconstraints prohibit arguments which are nullable value types.You could potentially create an interface without a constraint, but only create two implementations:
That wouldn’t stop anyone else from implementing
IFoo<int>of course. If you can give us more background, we may be able to help more.