views:

151

answers:

2

Just wondering where the rules for operators in C# are actually defined.

E.g. where can I see the code which says that == checks the references of two objects?

I can see the operator overloads in e.g. the String class but now i'm interested in seeing the 'base' case. Is it just something that the compiler explicitly knows what to do with and therefore there is no code which we can view using tools such as Reflector.

+9  A: 

You can't see it in code (except maybe in the SSCLI, I haven't checked).

You'll need to look at the C# language specification. For example:

7.10.6 Reference type equality operators

The predefined reference type equality operators are:

bool operator ==(object x, object y);
bool operator !=(object x, object y);

The operators return the result of comparing the two references for equality or non-equality.

Since the predefined reference type equality operators accept operands of type object, they apply to all types that do not declare applicable operator == and operator != members. Conversely, any applicable user-defined equality operators effectively hide the predefined reference type equality operators.

LukeH
+6  A: 

The == operator compiles down to a call to the ceq IL instruction.

Paul Ruane