Binary operators overloading in C#
Forums:
class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public static Point operator +(Point left, Point right) {
return new Point(left.x + right.x, left.y + right.y);
}
}
var p1 = new Point(1, 1);
var p2 = new Point(2, 2);
var p3 = p1 + p2;
//p3.x is 3 and p3.y is 3
p3 += new Point(3, 5);
//p3.x is 6 and p3.y is 8
- Log in to post comments