I was looking at the String and Int32 types through the reflector but couldn't find any operators that are defined.
If they aren't defined, how do +/-, etc works on these types?
The String class has only two, they have CLS compliant names: op_Equality and op_Inequality. The compiler has lots of built-in knowledge of the System.String class. Necessary in order to be able to use the Ldstr opcode for one. Likewise, it translates the + operator into String.Concat().
Pretty much the story for Int32, there are direct matches between operators and IL opcodes.
The numeric operators are part of IL itself. The "+" operator on strings is a bit special though - it's not overloaded by the string type itself, it's done by the compiler. The C# compiler translates:
string x = a + "hello" + b;
into:
string x = string.Concat(a, "hello", b);
This is more efficient than if the concatenation had been done using normal operators, because otherwise a new string would have to be created at each concatenation.
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