2

To use it like:

public class myClass <T> where T : IArithmetic
//...
public myFunction(T A, T B){
    //...
    X = A + B;
    //...
}

So, I must somehow overload + operator for the T class.

Denis Golovkin
  • 106
  • 2
  • 11

2 Answers2

2

This is not possible to create interface for implementing operators.

According to the Standard:

13.2 Interface members

The members of an interface must be methods, properties, events, or indexers. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types, nor can an interface contain static members of any kind.

awesoon
  • 32,469
  • 11
  • 74
  • 99
1

Can't create this restriction because the operator methods are static so they don't belong to the instance. You can do some runtime check with reflxion and throw a NotSupported excetion if the T type don't support the required operation.

Péter
  • 2,161
  • 3
  • 21
  • 30