Reputation: 2155
In C# you can overload operators, e.g. +
and *
. In their mathematical interpretation, these operators have a well defined order of precedence.
Is this order kept when overloading, does it change in some deterministic way, or can you overload the order of precedence as well?
Upvotes: 16
Views: 2933
Reputation: 44336
Overloading does not change precedence.
Operator precedence is set by the compiler, and cannot be changed, at least not without customizing the compiler.
Upvotes: 4
Reputation: 69372
If you overload an operator, it will always take precedence over the default implementation. However, you can't change the precedence of the operator itself, so it will be kept as default. More information on MSDN.
Relevant quotes:
User-defined operator implementations always take precedence over predefined operator implementations: Only when no applicable user-defined operator implementations exist will the predefined operator implementations be considered.
and
User-defined operator declarations cannot modify the syntax, precedence, or associativity of an operator. For example, the / operator is always a binary operator, always has the precedence level specified in Section 7.2.1, and is always left-associative.
Upvotes: 20