C # allows user-defined types to overload operators by defining static member functions using the operator keyword.
Attention:
- Must be decorated with public and must be a static method of the class.
- When you overload the equality operator (= =), you must also overload the inequality operation (! =).
- The < and > operators, as well as the <= and >= operators, must also be paired overloads.
Operators that can be overloaded:
- Unary operators that can be overloaded: + 、-、!、 ~, + + 、--、 true and False
- Binary operators that can be overloaded: +,-, *,/,%, &, |, ^, <<, >>
- Comparison operators that can be overloaded: = =,! =, <, <=;, >=
Operators that cannot be overloaded:
- &&, | | Conditional logical operators cannot be overloaded, but can be overloaded with & and | For calculation.
- [] Array index operators cannot be overloaded, but indexers can be defined.
- () You cannot overload conversion operators, but you can define new conversion operators (see explicit and implicit).
- + =,-=, *=,/=,%=, &=, |=, ^=, <<=, >>= assignment operators cannot be overloaded, but + = can use + COMPUTE
- = 、.、?:,,, New, is, sizeof, and TypeOf cannot overload these operators.
Attention:
- To overload an operator in a custom class, you need to create a method with the correct signature in the class. The method must be named "operator X", where X is the name or symbol of the operator being overloaded.
- A unary operator has a parameter, and a two-tuple operator has two parameters.
- In each case, the type of the parameter must be the same as the type of the class or struct that declares the operator.
1 /*design a plural class (Complex),2 * contains two fields (real x and imaginary part y),3 * Overloading operators to implement two4 * addition and subtraction of complex numbers,5 * and determine if two complex numbers are consistent6 */7 8 usingSystem;9 usingSystem.Collections.Generic;Ten usingSystem.Linq; One usingSystem.Text; A - namespaceHomeWork4 - { the class Program - { - Static voidMain (string[] args) - { + Complex com1, COM2, COM3; -COM1 =NewComplex (1,4); +COM2 =NewComplex (2,6); ACOM3 =NewComplex (); atCOM3 = com1 +COM2; -Console.WriteLine (COM1 +" + "+ COM2 +" = "+COM3); -COM3 = com1-COM2; -Console.WriteLine (COM1 +" - "+ COM2 +" = "+COM3); - if(COM1 = =COM2) -Console.WriteLine ("COM1 and COM2 consistent"); in Else -Console.WriteLine ("COM1 and COM2 inconsistent"); to + Console.readkey (); - } the } * $ classComplexPanax Notoginseng { - Public Doublex, y; the PublicComplex () +{x = y =0; } A PublicComplex (DoubleXDoubley) the { + This. x =x; - This. y =y; $ } $ Public StaticComplexoperator+(Complex comp1, Complex comp2) - { -Complex Comp =NewComplex (); thecomp.x = comp1.x +comp2.x; -COMP.Y = Comp1.y +comp2.y;Wuyi returncomp; the } - Public StaticComplexoperator-(Complex comp1, Complex comp2) Wu { -Complex Comp =NewComplex (); Aboutcomp.x = comp1.x-comp2.x; $COMP.Y = Comp1.y-comp2.y; - returncomp; - } - Public Override stringToString () A { + return "("+ x +","+ y +")"; the } - Public Static BOOL operator==(Complex C1, Complex C2) $ { the //Convert C1 object to object and then Judge the //here's the object. Equals (c1, NULL) can also be written as the //C1 As Object = = NULL the if(Object. Equals (C1,NULL) ||Object. Equals (C2,NULL)) - { in return Object. Equals (c1, C2); the } the About the returnc1.x = = c2.x && C1.y = =c2.y; the } the + - Public Static BOOL operator!=(Complex C1, Complex C2) the {Bayi if(Object. Equals (C1,NULL) ||Object. Equals (C2,NULL)) the { the return!Object. Equals (c1, C2); - } - the the returnc1.x! = c2.x | | C1.y! =c2.y; the } the }; -}
operator overloading in C #