开发者

Where are operators defined (in C#)?

开发者 https://www.devze.com 2023-01-17 05:44 出处:网络
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?

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 s开发者_如何学编程eeing 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消