Which Operators Can Be Overloaded?

Lots of operators support being overloaded by the developer of classes and structs. Although I could show examples of all of them, I’ll limit myself to illustrating a couple of special ones. We also discuss in this section how certain expressions that use operators are turned into uses of custom operators.

A Mandatory Listing

An exhaustive list of all the operators that can be overloaded is shown here. All but a few should look pretty straightforward. The others are the subject of further discussion in the following sections.

• Unary operators: +, -, ~, !, ++, --, true, false

• Binary operators: +, -, *, /, %, <<, >>, |, ^, <, <=, >, >=, ==, !=

Some operators are implemented in terms of the others. Examples ...

Get C# 4.0 Unleashed 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.