When you overload an operator, such as operator +
, the compiled CIL looks something like this:
.method public hidebysig specialname static bool op_Addition(...) { ... }
Why use the name op_Addition
here and not, say, the name +
?
I'm suggesting that the CIL syntax should have been
.method public hidebysig specialname static bool +(...) { ... }
And the member name, when looking for it, would have been +
rather than op_Addition
.
Note: This is a question about language design; "because the spec says so" is not a helpful answer.
If you named the method +
, then only languages which supported operator overloading, or which allowed +
as an identifier, would be able to call the underlying method.
The Unicode standard defines a syntax for identifiers in programming languages (see Annex 7), which is adopted by the CLS for the identifier rules, does not include +
in that syntax. Since most languages with support for Unicode identifiers will adhere to that syntax, it is unreasonable to expect such languages to allow +
as an identifier.
The CLR does not know about operators, so the c# compiler generates methods with thes special names so that other .net languages that do not have support for operators can consume them as methods.
CLR Via c# is a good reference for this type of question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With