The where T : struct
constraint lets one to limit the domain of acceptable type parameters to the set of value types (as compared to the superset of types including both value and reference types) only but also seems to forbid nullable types altogether although nullable doesn't necessarily mean a reference type in modern versions of C#.
What if I'd like to accept value types with added nullability like int?
, DateTime?
etc while rejecting natively-nullable reference types like string, IList etc? Is it possible to define the constraints this way? How if it is?
I am actually curious to learn to implement both the scenarios: when the type used as the parameter must be both value and nullable and when a nullable value type is to be accepted though as well as a non-nullable value type and I consider these related closely enough to excuse mentioning both so I'd appreciate a humble commentary about the second case and choose an answer including it as a better one (given another one is not going to be really better in other ways) if more than one answer will be submitted and I'll have to choose, but what I actually need right now is the first case (to always require a type that is both nullable and is a value type at the same time) and I also believe the second case is going to be pretty straightforward given the knowledge of the first, not to mention it is not a good manner to insist on gluing 2 questions into one so I will absolutely appreciate and accept an answer dealing with the first case only too.