I currently have a user defined type
class MyType {
double x;
double y;
double z;
//This class has operator overloading implemented
public static MyType operator + (double a,MyType b){
...
}
}
At some point later I will have an array (object[]) some of them double and some MyType. I would like to do evaluation over them but compiler is not allowing operator ‘+’ to be applied to type object and object. What should I do here?
When resolving a
+in code the compiler needs to bind it to a specific operation. The typeobjecthas no+operation and the compiler has no idea that the underlying types aredoubleandMyTypeand hence the compiler errors out.In order to fix this you will need to either
doubleandMyTypeso the compiler can properly bind the+operatordynamicand rely on the runtime binding to the correctoperator+.Here is an example of the latter method.