Overloadable operators (C# Programming Guide)

C# allows user-defined types to overload operators by defining static member functions using the operator keyword. Not all operators can be overloaded, however, and others have restrictions, as listed in this table:

Operators Overloadability
+, -, !, ~, ++, --, true, false These unary operators can be overloaded.
+, -, *, /, %, &, |, ^, <<, >> These binary operators can be overloaded.
==, !=, <, >, <=, >= The comparison operators can be overloaded (but see the note that follows this table).
&&, || The conditional logical operators cannot be overloaded, but they are evaluated using & and |, which can be overloaded.
[] The array indexing operator cannot be overloaded, but you can define indexers.
(T)x The cast operator cannot be overloaded, but you can define new conversion operators (see explicit and implicit).
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= Assignment operators cannot be explicitly overloaded. However, when you overload a binary operator, the corresponding assignment operator, if any, is also implicitly overloaded. For example, += is evaluated using +, which can be overloaded.
=, ., ?:, ??, ->, =>, f(x), as, checked, unchecked, default, delegate, is, new, sizeof, typeof These operators cannot be overloaded.

Note

The comparison operators, if overloaded, must be overloaded in pairs; that is, if == is overloaded, != must also be overloaded. The reverse is also true, where overloading != requires an overload for ==. The same is true for comparison operators < and > and for <= and >=.

For information about how to overload an operator, see the operator keyword article.

See Also