Section 12.5. Summary


12.5. Summary

  • You can overload operators in much the same way that you would overload methods .

  • To overload an operator, use the static keyword with the operator keyword, and the name of the operator you're overloading.

  • It is good programming practice to be parsimonious in your use of operator overloading, and to be sure that the meaning of the overload is obvious and intuitive.

  • When you overload the equals ( == ) operator, you should also overload the Equals( ) method for compatibility with other .NET languages.

  • If you overload the == operator, you must also overload the != operator. Similarly, the < and > operators are paired, as are the <= and >= operators.

  • You can also overload conversion operators to allow one type to be implicitly or explicitly cast to another type. When doing so, you must use the keyword implicit when the conversion is guaranteed to succeed without loss of information, and explicit when there is a risk that information might be lost.



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