Operator Overloading on Class

在C#設計Class,可以搭配運算子(operator)來做到多載(overloading) 並且,要Overloading的運算子,必須是 static 例如:

        class Box
        {
            public int Height { get; set; }
            public int Width { get; set; }

            public Box(int h, int w)
            {
                Height = h;
                Width = w;
            }
            public static Box operator +(Box a, Box b)
            {
                int h = a.Height + b.Height;
                int w = a.Width + b.Width;
                Box res = new Box(h, w);
                return res;
            }
        }

        static void Main(string[] args)
        {
            Box b1 = new Box(14, 3);
            Box b2 = new Box(5, 7);
            Box b3 = b1 + b2;
            Console.WriteLine(b3.Height);
            Console.WriteLine(b3.Width);
        }

底下範例中,實現多載 operator +, operator -, operator *, operator /

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {

        class Cacul
        {
            public double A;
            public double B;
            public Cacul(double x, double y)
            {
                A = x;
                B = y;
            }
            //constructor 使用 operator overloading +
            public static Cacul operator +(Cacul j, Cacul k)
            {
                double x = j.A + k.A;
                double y = j.B + k.B;
                Cacul z = new Cacul(x, y);
                return z;
            }
            //constructor 使用 operator overloading -
            public static Cacul operator -(Cacul j, Cacul k)
            {
                double x = j.A - k.A;
                double y = j.B - k.B;
                Cacul z = new Cacul(x, y);
                return z;
            }
            //constructor 使用 operator overloading *
            public static Cacul operator *(Cacul j, Cacul k)
            {
                double x = j.A * k.A;
                double y = j.B * k.B;
                Cacul z = new Cacul(x, y);
                return z;
            }
            //constructor 使用 operator overloading /
            public static Cacul operator /(Cacul j, Cacul k)
            {
                double x = j.A / k.A;
                double y = j.B / k.B;
                Cacul z = new Cacul(x, y);
                return z;
            }
        }
        static void Main(string[] args)
        {
            Cacul t1 = new Cacul(3, 4);
            Cacul t2 = new Cacul(5, 6);
            Cacul t3 = t1 + t2;
            Console.WriteLine(t3.A);//8
            Console.WriteLine(t3.B);//10
            Cacul t4 = t1 - t2;
            Console.WriteLine(t4.A);//8
            Console.WriteLine(t4.B);//10
            Cacul t5 = t1 * t2;
            Console.WriteLine(t5.A);//8
            Console.WriteLine(t5.B);//10
            Cacul t6 = t1 / t2;
            Console.WriteLine(t6.A);//8
            Console.WriteLine(t6.B);//10

        }
    }
}