Is there a constraint that restricts my generic method to numeric types?

后端 未结 21 2590
栀梦
栀梦 2020-11-21 05:48

Can anyone tell me if there is a way with generics to limit a generic type argument T to only:

  • Int16
  • Int32
21条回答
  •  孤街浪徒
    2020-11-21 06:16

    There is no 'good' solution for this yet. However you can narrow the type argument significantly to rule out many missfits for your hypotetical 'INumeric' constraint as Haacked has shown above.

    static bool IntegerFunction(T value) where T: IComparable, IFormattable, IConvertible, IComparable, IEquatable, struct {...

提交回复
热议问题