Recommendation 9: Custom overloaded operators
In the development process, you should be accustomed to using the syntax features that Microsoft provides to us. I think everyone likes to see this grammatical feature:
int 1 ; int 2 ; int
Instead of using the following syntax to do the same thing:
int 1 ; int 2 ; int int
Similarly, when building your own type, we should always consider whether the type can be used for operator overloading. If you consider type salary, the following code looks less comfortable:
New A }; New - };
Type support should be made:
The latter read at a glance. The CLR supports overloading operators in types by defining static member functions using the operator keyword, allowing developers to use the type as if they were built-in primitive types. The version of the salary overloaded "+" operator should look like the following:
class Salary { publicintgetset;} Public Static operator +(Salary s1, Salary S2) { + = S1. RMB; return s2; } }
Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia
157 recommendations for writing high-quality code to improve C # programs--Recommendation 9: Custom overloaded operators