This is what you are trying to do SEEM, but your example makes this difficult. So, from your comments in other answers, it looks like you want to add, subtract, multiply, divide Rational numbers, which means the result should also be Rational (not int).
That way, you can define each of your methods, and then implement statements to call them. Operators are always static, so you will need to check for null and handle accordingly (in this case, I just throw an ArgumentNullException ):
public class Rational { public Rational Add(Rational other) { if (other == null) throw new ArgumentNullException("other"); return // <-- return actual addition result here } public static Rational operator +(Rational left, Rational right) { if (left == null) throw new ArgumentNullException("left"); return left.Add(right); } public Rational Subtract(Rational other) { if (other == null) throw new ArgumentNullException("other"); return // <-- return actual subtraction result here } public static Rational operator -(Rational left, Rational right) { if (left == null) throw new ArgumentNullException("left"); return left.Subtract(right); } public Rational Multiply(Rational other) { if (other == null) throw new ArgumentNullException("other"); return // <-- return actual multiplication result here } public static Rational operator *(Rational left, Rational right) { if (left == null) throw new ArgumentNullException("left"); return left.Multiply(right); } public Rational Divide(Rational other) { if (other == null) throw new ArgumentNullException("other"); return // <-- return actual division result here } public static Rational operator /(Rational left, Rational right) { if (left == null) throw new ArgumentNullException("left"); return left.Divide(right); } }
source share