Operator overloading and precedence

前端 未结 2 1574
无人及你
无人及你 2021-01-17 10:39

In C# you can overload operators, e.g. + and *. In their mathematical interpretation, these operators have a well defined order of precedence.

相关标签:
2条回答
  • 2021-01-17 11:07

    Overloading does not change precedence.

    Operator precedence is set by the compiler, and cannot be changed, at least not without customizing the compiler.

    0 讨论(0)
  • 2021-01-17 11:08

    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.

    0 讨论(0)
提交回复
热议问题