运算符在哪里定义(在C#中)? [英] Where are operators defined (in C#)?

查看:219
本文介绍了运算符在哪里定义(在C#中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道C#中的操作符的规则实际上是在哪里定义。

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?

我可以看到运算符重载在例如。 String类,但现在我有兴趣看到'基地'的情况。这是编译器显式地知道该怎么做,因此没有代码,我们可以使用工具,如反射器。

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.

推荐答案

您无法在代码中看到它(除了可能 SSCLI ,我尚未选中)。

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

需要查看 C#语言规范。例如:


7.10.6引用类型相等运算符

预定义的引用类型等于
运算符是:

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.

由于预定义的引用类型
相等运算符接受
的操作数类型 object ,它们适用于所有未声明适用的类型

运算符 == 和运算符!=
成员。相反,任何适用的
用户定义的等式运算符
有效地隐藏预定义的
引用类型相等运算符。

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.

这篇关于运算符在哪里定义(在C#中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆