Section 12.2. Creating Useful Operators


12.2. Creating Useful Operators

Operator overloading can make your code more intuitive and enable it to act more like the built-in types. However, if you break the common idiom for the use of operators, operator overloading can make your code unmanageably complex and obtuse. Therefore you should always resist the temptation to use operators in new and idiosyncratic ways.

For example, although it might be tempting to overload the increment operator ( ++ ) on an employee class to invoke a method incrementing the employee's pay level, this can create tremendous confusion. The increment operator normally means "increase this scalar value by one." Giving it the new meaning of "increase this employee's pay level" will be obvious to the person implementing the operator, but may be very confusing to you or other programmers who have to maintain the code. It is best to use operator overloading sparingly, and only when its meaning is clear and consistent with how the built-in classes operate .



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net