Chapter 12: Operator Overloading

Quiz Solutions

Solution to Question 12-1. Operator overloading is the process of writing methods for your class that allow clients of your class to interact with your class using standard operators (such as + and ==).

Solution to Question 12-2. Operators are implemented as static methods.

Solution to Question 12-3. To overload an operator, you use the keyword operator along with the operator you’re overloading. For example, to overload the addition operator, you would use the keyword operator+.

Solution to Question 12-4. The compiler interprets the statement as a call to the method:

public static Fraction operator+(f2, f1)

Solution to Question 12-5. This answer is subjective, but it seems likely that choices A and D are the most reasonable. Choices B and C are not completely unreasonable, but aren’t intuitive, and would be difficult for later developers to maintain.

Solution to Question 12-6. The < and > operators are paired, as are the <= and >= operators. If you overload one of the operators in a pair, you must overload the other.

Solution to Question 12-7. If you overload the == operator, you must also overload the != operator, and the Equals( ) method.

Solution to Question 12-8. The Equals( ) method is used to ensure that your class is compatible with other .NET languages that do not allow operator overloading, but do allow method overloading.

Solution to Question 12-9. To overload the conversion operators, you use either the keyword implicit or the keyword ...

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.