24

Is it possible to constraint a method so that it receives only int, double, long or other numerical types that has the usual numerical operations ( such as +,-,*,/) defined?

Graviton
  • 81,782
  • 146
  • 424
  • 602
  • 2
    You could probably create an interface with those operations, and wrap the numerical data types in something that implements that interface. It probably wont be the most efficient, and will use a bunch of custom stuff, but it will solve that problem if that is the most important thing. – Nick Larsen Apr 15 '10 at 12:53

2 Answers2

21

This is not possible currently.

Also, see this question for a possible workaround

Community
  • 1
  • 1
rpetrich
  • 32,196
  • 6
  • 66
  • 89
17

No, unfortunately there is no such restriction possible. The closest you can get is using struct, IConvertible as generic type constraint.

You may want to read this question though.

Community
  • 1
  • 1
Lucero
  • 59,176
  • 9
  • 122
  • 152
  • 2
    I am surprised as to why this from Lucero was not voted. Using structs is the closest one can restrict the type parameter to be of value types – AlwaysAProgrammer Apr 15 '10 at 13:22