简体   繁体   中英

Where are operators defined (in C#)?

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

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

I can see the operator overloads in eg 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.

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.

==运算符可向下编译为对ceq IL指令的调用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM