c# 定義operator運算符
阿新 • • 發佈:2018-05-31
() pub TE HA shc span sta clas !=
public void TestFunc() { Complex complex1 = new Complex(); Complex complex2 = new Complex(); var s = complex1 + complex2; } } public class Complex { public string Name { get; set; } public int Age { get; set; }public Complex() { } // 必須在當前類中定義 public static Complex operator +(Complex a, Complex b) { return new Complex() { Name = a.Name + b.Name, Age = a.Age + b.Age }; } public static string operator -(Complex a, Complex b) {return null; } //public static Boolean operator ==(Complex a, Complex b) //{ // return true; //} //public static Boolean operator !=(Complex a, Complex b) //{ // return false; //} //public override bool Equals(object obj) //{// return base.Equals(obj); //} //public override int GetHashCode() //{ // return base.GetHashCode(); //} } }
c# 定義operator運算符