22.2. Operator overloading proper
After seeing an example, you should have a good idea of what operator overloading is all about. Now, let's go into the specifics. The operator [2] method has to be defined like this:
[2] operator is a C# keyword.
public static <return_value> operator <operator to overload> (<operand1>, [<operand2>]);
The following lists some rules that need to be followed when overloading an operator.
You cannot overload just any C# operator, and you cannot define your own operator. Only the operators shown in Table 22.1 can be overloaded.
Table 22.1. Operators that can be overloaded in C# Category Operators Unary operators + – ! ~ ++ -- true false Binary operators + – * / % and | ^ << >> Comparison operators == != < > <= >= The +=, ...
Get From Java to C#: A Developer's Guide 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.