General class with limited type parameter

I want to create a universal class that takes a type parameter and restricts this parameter to numeric types or, more generally, to any type on which the increment operator ++ can be applied.

I know that I can do the following to restrict structures, but obviously there are structures that are not numeric types and for which the ++ operator is not supported. Can i do this in c #

class Example<T> where T : struct
{
  //Implementation detail
}
+5
source share
1 answer

Unfortunately, this is not possible (see here .) You can only restrict the type:

  • Embed a specific interface or get a specific class
  • Be classorstruct

, , , , # 4.

+8

All Articles