Operator overloading: +, - : operator overload Binary Plus Subtract « Operator Overload « C# / CSharp Tutorial






using System;

public class Point
{
    public Point( int x, int y )
    {
        this.X = x;
        this.Y = y;
    }

    public int X;

    public int Y;

    public static Point operator + ( Point a, Point b )
    {
        return new Point( a.X + b.X, a.Y + b.Y );
    }

    public static Point operator - ( Point a )
    {
        return new Point( - a.X , - a.Y );
    }

    static void Main(string[] args)
    {
        Point p = new Point( 3, 4 );
        Point q = new Point( 36, -5 );

        Point r = p + ( - q );

        System.Console.WriteLine( "Result: x = {0}, y = {1}", r.X, r.Y );
    }
}
Result: x = -33, y = 9








8.9.operator overload Binary Plus Subtract
8.9.1.Add + operator for Complex type
8.9.2.Operator overloading: +, -
8.9.3.An operator overloading: binary + and -