Appendix A: Operator Overloading

Appendix A: Operator Overloading


Developers add operator overloading to user-defined types to describe how that type works with common operators. A feature of built-in types is the automatic support of most operators. Built-in types, such as int, float, and long-value types, behave intuitively with mathematical, logical, and conditional operators. As expected, the plus, minus, and multiply operators implicitly perform addition, subtraction, and multiplication when applied to these types. This intrinsic knowledge of operators makes using built-in types convenient. You might want to use user-defined types as conveniently. Unfortunately, the C# compiler cannot interpret user-defined classes in the context of most of the standard operators. For example, how does the compiler interpret zclass+zclass? The compiler cannot perform this interpretation without help from the class developer. This assistance is offered through operator overloading, which describes an operator in the context of a particular type. Overload several operators in a user-defined type to provide the same transparency found in most built-in types for operators.

User-defined types that represent numerical systems or perform mathematical operations benefit the most from operator overloading. A fraction, complex number, summation, hexadecimal numerical system, and other similar types that have well-established behavior are improved with operator overloading.

You can also describe type conversion of user-defined types using operator overloading. Conversion operators can convert or cast a user-defined type to another type, which can be a built-in type or another user-defined type. With some exceptions, you can cast from one built-in type to another built-in type. For example, you can implicitly cast from a short to a long value. You can also cast explicitly from a long to a short value. However, what does it mean to cast a ZClass instance to an int value? This is another situation in which developer input is essential. Developers overload conversion operators to instruct the compiler in the conversions of user-defined types.

Operator overloading should be intuitive and is never required. Do not stray from the underlying intent of the operator. For example, the plus operator should perform some type of addition. System.String overloads the operator+ to concatenate strings. Concatenation and addition are similar concepts. If the implementation of the operator+ within the string type truncated characters, that would be nonintuitive and more confusing than helpful. Operator overloading can be overused and is not appropriate in all circumstances. The operator+ is a reasonable addition to a string class. However, an operator-, although available, would be nonsensical. Remember, the overloaded operator must be intuitive to everyone, not just to you. Confirm with peers your perception of intuitive behavior. An alternative to operator overloading is simply implementing a named method: Add method instead of an operator+ and Subtract method instead of overloading the operator-. There is the added benefit that named methods are called overtly, whereas operator functions are called tacitly.