O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

  1. 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#
    CategoryOperators
    Unary operators+ – ! ~ ++ -- true false
    Binary operators+ – * / % and | ^ << >>
    Comparison operators== != < > <= >=
  2. The +=, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required