Like most programming languages. C# also gives us the ability to define our own implementation of an operator. Although it can be confusing to see in action, operator overloading as it is called, is a useful feature that you can easily use to provide custom arithmetic or comparison operations in a class.
Note: | The ability to create your own operator is very much in tune with C++. Java does not natively support this functionality, as there is some need for a preprocessor. However, there packages available to allow this type of development with Java as well. |
Almost any unary or binary operator can be overridden with a default behavior. Some operators require that other operators be implemented as well. For example, if the less than (<) operator is defined then the greater than (>) must be defined as well.
There are also a few rules for defining operators. First, the signature of the operator must always be public static. Second, only value types can be passed to the operator. Attempting to pass a reference type will throw a compile-time error. Operators are covered in more detail in Chapter 4.