8.18. Operator Overloading

We have mentioned that certain operators can be overloaded and certain other operators cannot be overloaded. Overloading an operator means to customize its behavior depending on the context in which it is being used. For example, the additive (+) operator is used for adding numerical values when used in the numeric context (that is, both operands are numeric data types). However, when one or more operands of this operator are strings, the operation is treated as string concatenation. You can say that the + operator is overloaded for strings to concatenate them.

Overloading operators makes sense if the context in which they are used is intuitive to the end user. For example, strings do not overload the multiplicative ...

Get .NET for Java Developers: Migrating to C# 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.