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

后端 未结 21 2673
栀梦
栀梦 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:35

    I was wondering the same as samjudson, why only to integers? and if that is the case, you might want to create a helper class or something like that to hold all the types you want.

    If all you want are integers, don't use a generic, that is not generic; or better yet, reject any other type by checking its type.

提交回复
热议问题