Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use the name 'op_Addition' for operator '+' rather than the name '+'?

Tags:

c#

cil

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.

like image 694
configurator Avatar asked Oct 21 '11 14:10

configurator


2 Answers

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.

like image 164
Michael Madsen Avatar answered Nov 02 '22 04:11

Michael Madsen


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.

like image 24
Kell Avatar answered Nov 02 '22 03:11

Kell