C#运算符重载

  operator 关键字用于在类或结构声明中声明运算符。运算符声明可以采用下列四种形式之一:
  public static result-type operator unary-operator ( op-type operand )
  public static result-type operator binary-operator (
   op-type operand,
   op-type2 operand2
  )
  public static implicit operator conv-type-out ( conv-type-in operand )
  public static explicit operator conv-type-out ( conv-type-in operand )
  示例
  以下是一个有理数的极其简化的类。该类重载 + 和 * 运算符以执行小数加法和乘法,同时提供将小数转换为双精度的运算符。
  using System;
  class Fraction
  {
   int num, den;
   public Fraction(int num, int den)
   {
   this.num = num;
   this.den = den;
   }
   // overload operator +
   public static Fraction operator +(Fraction a, Fraction b)
   {
   return new Fraction(a.num * b.den + b.num * a.den,
   a.den * b.den);
   }
   // overload operator *
   public static Fraction operator *(Fraction a, Fraction b)
   {
   return new Fraction(a.num * b.num, a.den * b.den);
   }
   // define operator double
   public static implicit operator double(Fraction f)
   {
   return (double)f.num / f.den;
   }
  }
  补充
  参数
  result-type 运算符的结果类型。
  unary-operator 下列运算符之一:+ - ! ~ ++ — true false
  op-type 第一个(或唯一一个)参数的类型。
  operand 第一个(或唯一一个)参数的名称。
  binary-operator 其中一个:+ - * / % & | ^ << >> == != > < >= <=
  op-type2 第二个参数的类型。
  operand2 第二个参数的名称。
  conv-type-out 类型转换运算符的目标类型。
  conv-type-in 类型转换运算符的输入类型。
  =号不可以重载

你可能感兴趣的:(运算符)