c++中可以分别重载前缀和后缀的自增操作符,但是c#中应该怎么重载呢?

解决方案 »

  1.   

    参考:
    public struct Complex
    {
        public int real;
        public int imaginary;    public Complex(int real, int imaginary)  //constructor
        {
            this.real = real;
            this.imaginary = imaginary;
        }    // Declare which operator to overload (+),
        // the types that can be added (two Complex objects),
        // and the return type (Complex):
        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
        }    // Override the ToString() method to display a complex number in the traditional format:
        public override string ToString()
        {
            return (System.String.Format("{0} + {1}i", real, imaginary));
        }
    }