I'm defining a generic type:
public class Point<T> where T : IConvertible, IComparable
What I would really like to do is constrain T to be a numeric type (one of the ints or floats.) There's no INumeric in the CLR. Is there an interface or collection of interfaces that could be used here to constrain the type to one of the boxed numeric classes?
Unfortunately, no. This has been a highly requested feature for a long time.
Right now, the best option is likely to use:
where T : struct, IConvertible, IComparable<T>
(The struct constraint prevents string usage...)
However, this still allows any user defined value type that implements the appropriate constraints to be used.
where T: struct
will constrain it to a value type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With