Проблема с operator +
Согласно MSDN, компилятор выдает ошибку, если ни один из параметров в методе operator +
не относится к типу класса, в который записан метод. Ссылка на документацию .
class iii { //this is extracted from the link above.. this is not complete code.
public static int operator +(int aa, int bb) ... // Error CS0563
// Use the following line instead:
public static int operator +(int aa, iii bb) ... // Okay.
}
Этот код будет работать, потому что вы конвертируете хотя бы один из параметров в ex
тип:
class basic { }
class symbol : basic { }
class numeric : basic { }
class ex {
public static implicit operator ex(basic b) {
return new ex();
}
public static implicit operator basic(ex e) {
return new basic();
}
public static ex operator + (basic lh, ex rh) {
return new ex();
}
}
class Program {
static void Main(string[] args) {
symbol s = new symbol();
numeric n = new numeric();
// ex e0 = s + n; //error!
ex e1 = (ex)s + n; //works
ex e2 = s + (ex)n; //works
ex e3 = (ex)s + (ex)n; //works
}
}