C# allows user-defined types to overload operators by defining static member functions using the operator keyword. Not all operators can be overloaded.
These unary operators can be overloaded : +, -, !, ~, ++, --, true, false
These binary operators can be overloaded : +, -, *, /, %, &, |, ^, <<, >>
The comparison operators can be overloaded : ==, !=, <, >, <=, >=
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: [ ]
The cast operator cannot be overloaded, but you can define new conversion operators :( )
Assignment operators cannot be overloaded, but +=, for example, is evaluated using +, which can be overloaded : +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
These operators cannot be overloaded : =, ., ?:, ->, new, is, sizeof, typeof
These unary operators can be overloaded : +, -, !, ~, ++, --, true, false
These binary operators can be overloaded : +, -, *, /, %, &, |, ^, <<, >>
The comparison operators can be overloaded : ==, !=, <, >, <=, >=
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: [ ]
The cast operator cannot be overloaded, but you can define new conversion operators :( )
Assignment operators cannot be overloaded, but +=, for example, is evaluated using +, which can be overloaded : +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
These operators cannot be overloaded : =, ., ?:, ->, new, is, sizeof, typeof
In C#, an operator is a term or a symbol that takes one or more expressions, called operands, as input and returns a value. Operators that take one operand, such as the increment operator (++) or new, are called unary operators. Operators that take two operands, such as arithmetic operators (+,-,*,/) are called binary operators. One operator, the conditional operator (?:), takes three operands and is the sole tertiary operator in C#.
Primary:
x.y, f(x), a[x], x++, x--, new, typeof, checked, unchecked
Unary:
+, -, !, ~, ++x, --x, (T)x
Arithmetic — Multiplicative:
*, /, %
Arithmetic — Additive:
+, -
Shift:
<<, >>
Relational and type testing:
<, >, <=, >=, is, as
Equality:
==, !=
Logical, in order of precedence :
&, ^, |
Conditional, in order of precedence:
&&, ||, ?:
Assignment:
=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
Primary:
x.y, f(x), a[x], x++, x--, new, typeof, checked, unchecked
Unary:
+, -, !, ~, ++x, --x, (T)x
Arithmetic — Multiplicative:
*, /, %
Arithmetic — Additive:
+, -
Shift:
<<, >>
Relational and type testing:
<, >, <=, >=, is, as
Equality:
==, !=
Logical, in order of precedence :
&, ^, |
Conditional, in order of precedence:
&&, ||, ?:
Assignment:
=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=