tags:

views:

243

answers:

5

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?

+5  A: 

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.

Hans Passant
Whoah... you're here too?
David Morton
Thanks nobugz. Good to see you here. If that's the case, one cannot recreate these types from scratch, right?
Joan Venge
@David: It's nice to see familiar names.
Joan Venge
@Joan: No, the compiler and spec have definite knowledge of these types. You could write your own types which have similar displayed behaviour, but without the optimisations :(
Jon Skeet
Thanks Jon. So can you say, for other .NET languages, either one has to do this in their own compiler like MS, or this can't be done and so no .NET language can beat C# in speed?
Joan Venge
Not for these operations. There are other things you can do in C++/CLI which you can't do in C# though, like changing values in boxed structs (without there being an interface involved).
Jon Skeet
Thanks Jon, very useful.
Joan Venge
+10  A: 

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.

Jon Skeet
+1  A: 

Yes, these operations are translated to native IL instructions, not calling the "operator+" method explicitly. Its probably not managed code performing these actions...

m0rb
+1  A: 

The c# compiler is a crazy son of a b...I once tried to recreate the abilities of the Nullable type and was unable until in some comment on his blog Eric Lippert reassured me that its capabilities are also derived from what the compiler generates when it encounters nullable types.

flq
+2  A: 

The operators for primitives are a pain, as I found when trying to write generic support for operators (i.e. using T + T etc); there is also a discussion page here that covers this.

You can get around the issue by using abstractions such as Expression (.NET 3.5) - otherwise, you'll have to look at raw IL, or use a handful of known methods.

Marc Gravell
Thanks Marc. When you said "you'll have to look at raw IL, or use a handful of known methods.", did you mean by doing these, you can solve the T + T problem? If so how? Just wondering.
Joan Venge
@Joan: Yes. Follow Marc's two links. It's very cool stuff :)
Jon Skeet
(It doesn't solve the syntax part, but it does solve the usage part.) Unfortunately my web server seems to be down at the moment, so you *can't* follow the links, apparently. Will look into it.
Jon Skeet
Re "raw IL", I mean you'd mave to look into things like `Reflection.Emit` and `OpCodes.Add`; it is a **lot** easier to use `Expression` (or just borrow `Operator` from MiscUtil).
Marc Gravell