We know that for some basic data types (int32, long, double, etc.) We can perform some operations through +,-, *, but for some of the complicated types defined by ourselves, if we want to implement these functions, we can only define some methods to perform these operations, which is certainly not intuitive. If you want to use these operators in your own class, you must inform the compiler of the meaning of the operators in this class and the operation logic, and then use the operator overload.
The reload of operators is not limited to arithmetic operators. Some comparison operators =, <,>, and! Must be considered during the reload ,! =, >=, <=, And so on. For example, if (A = B), for classes, by default, this statement compares the references of these two objects to the same address, instead of comparing the field values of the two objects to see if they are equal. In addition, for the structure, the = operator does not work by default. If you try to compare the two structures to see if they are equal, A compilation error is generated. To compare the structure, we must display the overload = Operator to tell the compiler how to compare it. The following describes how to use code.
I. Arithmetic Operators +,-, *,/overload
View code
Namespace consoleapp {class program {static void main (string [] ARGs) {Point P1 = new point (10, 8); point P2 = new point (5, 4 ); point P3 = p1 + P2; point P4 = p1-P2; point P5 = p1 * P2; point P6 = p1/P2; console. writeline ("P1-" + p1); console. writeline ("P2-" + p2); console. writeline ("P3-" + P3); console. writeline ("P4-" + P4); console. writeline ("P5-" + P5); console. writeline ("P6-" + P6) ;}} public class point {private int32 X; private int32 y; Public point () {This. X = 0; this. y = 0;} public point (int32 _ x, int32 _ y) {This. X = _ x; this. y = _ y;} public int32 X {get {return X;} set {x = value ;}} public int32 y {get {return y ;} set {Y = value ;}//< summary> /// overload + operator /// </Summary> Public static point operator + (point P1, point P2) {return new point (p1.x + p2.x, p1.y + p2.y );} /// <summary> /// overload-operator /// </Summary> Public static point operator-(point P1, point P2) {return new point (p1.x-p2.x, p1.y-p2.y);} // <summary> // overload * operator // </Summary> Public static point operator * (point P1, point P2) {return new point (p1.x * p2.x, p1.y * p2.y );} /// <summary> /// overload/operator /// </Summary> Public static point operator/(point P1, point P2) {If (p2.x = 0 | p2.y = 0) throw new exception ("the number of divisor cannot be 0"); return new point (p1.x/p2.x, p1.y/p2.y);} public override string tostring () {return string. format ("X: {0}, Y: {1}", this. x, this. Y );}}}
Ii. Comparison Operator Overloading
View code
Namespace consoleapp {class program {static void main (string [] ARGs) {Point P1 = new point (10, 8); point P2 = new point (5, 4 ); point P3 = new point (5, 4); console. writeline ("P1> P2:" + (P1> P2 ). tostring (); console. writeline ("p2 <P1:" + (P2 <P1 ). tostring (); console. writeline ("p2 = P3:" + (P2 = P3 ). tostring () ;}} public class point {private int32 X; private int32 y; Public point () {This. X = 0; this. y = 0;} public point (int32 _ x, int32 _ y) {This. X = _ x; this. y = _ y;} public int32 X {get {return X;} set {x = value ;}} public int32 y {get {return y ;} set {Y = value ;}} # region comparison operator overload // <summary> // overload equal operator // </Summary> Public static boolean operator = (point P1, point P2) {return (p1.x = p2.x) & (p1.y = p2.y) ;}/// <summary> /// overload unequal operators /// </Summary> publi C static boolean operator! = (Point P1, point P2) {return! (P1.x = p2.x) & (p1.y = p2.y );} /// <summary> /// overload> operator /// </Summary> Public static boolean operator> (point P1, point P2) {return (p1.x * p1.x + p1.y * p1.y)> (p2.x * p2.x + p2.y * p2.y );} /// <summary> /// overload <operator /// </Summary> Public static boolean operator <(point P1, point P2) {return (p1.x * p1.x + p1.y * p1.y) <(p2.x * p2.x + p2.y * p2.y );} /// <summary> /// overload> = Operator /// </Summary> Public static boolean operator> = (point P1, point P2) {return (p1.x * p1.x + p1.y * p1.y) >=( p2.x * p2.x + p2.y * p2.y );} /// <summary> /// overload <= Operator /// </Summary> Public static boolean operator <= (point P1, point P2) {return (p1.x * p1.x + p1.y * p1.y) <= (p2.x * p2.x + p2.y * p2.y);} # endregion public override string tostring () {return string. format ("X: {0}, Y: {1}", this. x, this. Y );}}}